Reputation: 159
I have a directory that organizes files into other directories named after ID numbers. Each of these directories contains icons as well as animation frames for graphical objects. I need to keep all files containing the string "icon"
Here is an example of a directory:
0501
| 05010000.effect.default.0.png
| 05010000.effect.default.1.png
| 05010000.effect.default.2.png
| 05010000.effect.default.3.png
| 05010000.effect.default.icon.png
| 05010000.effect.default.iconRaw.png
Afterwards, the directory should look like:
0501
| 05010000.effect.default.icon.png
| 05010000.effect.default.iconRaw.png
What would be even better is if I could also rename items in the directory to:
0501
| 5010000.png
| 5010000raw.png
respecting the original ID number for each image and deleting the leading 0.
This may not even be possible, but I have thousands of images that I need to do this for, so a manual process is not feasible.
Upvotes: 1
Views: 1219
Reputation: 9618
The script below will do the deleting and the renaming both. But the substitution used to create the new filename for the rename is case sensitive so the filenames must be lower case like in your example.
This doesn't actually do the deleting and renaming - it just prints the commands to the screen. Remove the ECHO in front on the DEL and REN commands to have the commands executed.
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
FOR /F "tokens=*" %%G IN ('DIR /S /B *.png ^| FINDSTR /V icon') DO (
ECHO DEL %%G
)
FOR /F "tokens=*" %%G IN ('DIR /S /B *.png ^| FINDSTR icon') DO (
SET X=%%~nG
SET Y=!X:.info.icon=!
ECHO REN %%G !Y:~1!%%~xG
)
The first for loop deletes the incorrect files (identical to the answer by @zb226) and the second loop renames the files we are keeping.
How it works is:
FOR /F "tokens=*" %%G IN ('DIR /S /B *.png ^| FINDSTR /V icon') DO (
...
)
The FOR executes the command in single quotes which finds the files to delete and then runs the commands after the DO once for each filename found (with the filename in %%G
). The command that finds the files uses DIR to do a directory listing (/B returns just the filename and /S recurses the subdirectories) and then uses FINDSTR to filter the filenames by returning filenames that don't contain icon
(/V returns things that don't match).
FOR /F "tokens=*" %%G IN ('DIR /S /B *.png ^| FINDSTR icon') DO (
...
)
The FOR executes the command in single quotes which finds the files to rename and then runs the commands after the DO once for each filename found (with the filename in %%G
). The command that finds the files uses DIR to do a directory listing (/B returns just the filename and /S recurses the subdirectories) and then uses FINDSTR to filter the filenames by returning filenames that do contain icon
SET X=%%~nG
SET Y=!X:.info.icon=!
REN %%G !Y:~1!%%~xG
Lots of stuff is happening here:
X=%%~nG
assigns just the filename stored in %%G
to X!X:.info.icon=!
removes .info.icon
from the filename.!Y:~1!
removes the first character (the leading zero), so !Y:~1!
is the new filename and %%~nG
is the old filename.%%G
is the full drive, path, filename and extension of the old file and !Y:~1!%%~xG
builds the filename and extension of the new file.Upvotes: 1
Reputation: 10500
The following batch file needs to be executed from the folder containing all your folders like the example folder 0501
. The script does not actually delete anything yet, it just prints out the deletion commands. Remove the ECHO
when you're confident that the solution does what you want.
@ECHO OFF
FOR /F "tokens=*" %%G IN ('DIR /S /B *.png ^| FINDSTR /V /I icon') DO (
ECHO DEL %%G
)
Upvotes: 1