I have a work folder. In that folder there are multiple sub folders. Each subfolder has 100 png files.
The 100 png files have a name which includes X and Y coordinates, which represent a position in a 10x10 grid.
Each png has a name format like this [ArbitraryFileName][xCoordinate][yCoordinate].png. The coordinates range from 0 to 9. Examples:
.../Root Folder/Layer 0/layer0_x00_y00.png
.../Root Folder/Layer 0/layer0_x01_y09.png
.../Root Folder/ArbitraryFolderName/AbitraryFileName_x01_y09.png
.../Root Folder/Layer 14/layer14_x00_y00.png
.../Root Folder/Layer 43/layer43_x00_y00.png
All I need the script to do is rename the files and reverse the order of the Y coordinate for each set.
layer0_x00_y00.png would be renamed to layer0_x00_y09.png
layer0_x00_y01.png would be renamed to layer0_x00_y08.png
layer0_x00_y02.png would be renamed to layer0_x00_y07.png
layer0_x00_y03.png would be renamed to layer0_x00_y06.png
layer10_x09_y09.png would be renamed to layer10_x09_y00.png
RandomFile_x00_y00.png would be renamed to RandomFile_x00_y09.png
and so on.
All I need to do is
- Run the script file in the root folder
- Have it rename and reverse the order of the Y coordinate for all of the images in all of the subfolders within the root folder.
I've very basic understanding in batch script, so I put together following code for my need but it doesn't iterate through sub folders and it doesn't work when it is used once again. What am I doing wrong? Can some one help me on this?
@echo off
setlocal enabledelayedexpansion
for /R %%a in (*_*.png) do (`
set file=%%a
ren "!file!" "!file:_=M!"
)
pause
setlocal enabledelayedexpansion
SET old1=My00
SET new1=_y09
SET old2=My01
SET new2=_y08
SET old3=My02
SET new3=_y07
SET old4=My03
SET new4=_y06
SET old5=My04
SET new5=_y05
SET old6=My05
SET new6=_y04
SET old7=My06
SET new7=_y03
SET old8=My07
SET new8=_y02
SET old9=My08
SET new9=_y01
SET old10=My09
SET new10=_y00
for /R "tokens=*" %%f in ('dir /b *.png') do (
SET newname=%%f
SET newname=!newname:%old1%=%new1%!
SET newname=!newname:%old2%=%new2%!
SET newname=!newname:%old3%=%new3%!
SET newname=!newname:%old4%=%new4%!
SET newname=!newname:%old5%=%new5%!
SET newname=!newname:%old6%=%new6%!
SET newname=!newname:%old7%=%new7%!
SET newname=!newname:%old8%=%new8%!
SET newname=!newname:%old9%=%new9%!
SET newname=!newname:%old10%=%new10%!
move "%%f" "!newname!"
)
pause
setlocal enabledelayedexpansion
SET oldx=Mx0
SET newx=_x0
for /R "tokens=*" %%f in ('dir /b *.png') do (
SET newname=%%f
SET newname=!newname:%oldx%=%newx%!
move "%%f" "!newname!"
)
pause
)
FYI,
Scenario 01:
(i) 1st FOR LOOP
with /R
& (*_*.png)
(ii) 2nd FOR LOOP
with /R
& (*.png)
(iii) 3rd FOR LOOP
with /R
& (*.png)
Result : No changes at all. No files renamed in any way.
Scenario 02:
(i) 1st FOR LOOP
with /R
& (*_*.png)
(ii) 2nd FOR LOOP
with /F
& ('dir /b *.png')
(iii) 3rd FOR LOOP
with /F
& ('dir /b *.png')
Result : (i) "The syntax of the command is incorrect" (ii) "File not found". (iii) "File not found".
Scenario 03:
(i) 1st FOR LOOP
with /R
& (*_*.png)
(ii) 2nd FOR LOOP
with /F
& ('dir /b /s *.png')
(iii) 3rd FOR LOOP
with /F
& ('dir /b /s *.png')
Result : (i) "The syntax of the command is incorrect" (ii) Says "1 File moved" to each file. (iii) Says "1 File moved" to each file. But no changes in the files names.
CodePudding user response:
With this you can apply rules for X too:
@ECHO off
SETLOCAL enableDelayedExpansion
FOR /R %%F in (*_*.png) DO (
FOR /F "usebackq tokens=1,2,3 delims=_." %%w in ('%%F') do (
SET x=%%x
SET x=!x:x=!
SET y=%%y
SET /a y=1!y:y=!-100
SET /a y=9-!y!
SET y=0!y!
MOVE "%%F" "%%w_x!x!_y!y!%%~xF"
)
)
Explanation:
> FOR /R %%F in (*_*.png)
All "_.png" files and SYMLINKs in the directory tree.
FOR /F
Changes the treatment of parameters according to the quotes, literal is "string".
"usebackq"
New Changes the treatment of parameters enabling other resources. Now literal is 'string'.
tokens=1,2,3 delims=_." %%w
Set %%w as full file name before the first '_' delimiter.
And cut off after the extension dot.
Following the sequence 1:w before _, 2:x between _, 3:y between _ and .
('%%F')
The PNG full path and name as literal string
SET y=!y:y=!
Cuts off the character y from y00 for example. Leaving only numbers.
This is an resource of the variables.
SET /a y=1!y:y=!-100
Cheat the question about octal numbers in MSDOS.
SET /a y=9-!y!
Now you have only decimal numbers, apply your regules.
%%~xF"
The extension of the file (.png).
Debug code for illustration:
@ECHO off
SETLOCAL enableDelayedExpansion
FOR /R %%F in (*_*.png) DO (
FOR /F "usebackq tokens=1,2,3 delims=_." %%w in ('%%F') do (
SET x=%%x
SET x=!x:x=!
SET y=%%y
SET /a y=1!y:y=!-100
SET /a y=9-!y!
SET y=0!y!
REM Debug your rules:
ECHO x: %%x to !x!, y: %%y to !y!
REM Echo your renaming:
ECHO RENAME "%%F" "%%w_x!x!_y!y!%%~xF"
)
)
Output:
> changeAll.bat
x: 00 to 00, y: 00 to 09
RENAME "C:\stack\bar\layer0_x00_y00.png" "C:\stack\bar\layer0_x00_y09.png"
x: 00 to 00, y: 01 to 08
RENAME "C:\stack\bar\layer0_x00_y01.png" "C:\stack\bar\layer0_x00_y08.png"
x: 00 to 00, y: 02 to 07
RENAME "C:\stack\bar\layer0_x00_y02.png" "C:\stack\bar\layer0_x00_y07.png"
x: 00 to 00, y: 01 to 08
RENAME "C:\stack\foo\layer0_x00_y01.png" "C:\stack\foo\layer0_x00_y08.png"
x: 00 to 00, y: 02 to 07
RENAME "C:\stack\foo\layer0_x00_y02.png" "C:\stack\foo\layer0_x00_y07.png"
x: 00 to 00, y: 08 to 01
RENAME "C:\stack\bar\layer0_x00_y08.png" "C:\stack\bar\layer0_x00_y01.png"