Edit: Just found out this is not an uncommon question, and my question was flagged duplicate of At which point does `for` or `for /R` enumerate the directory (tree)?
Question:
I have 2 files where I want to replace _
with _1
with a batch file using REN.
IMG_0002.JPG --> IMG_10002.JPG
IMG_0013.JPG --> IMG_10013.JPG
I am using this script:
for %%a in (IMG_*.*) do call :next %%a
pause
GOTO:EOF
:next
set newname=%1
set newname=%newname:_=_1%
ren %1 %newname%
The problem is, that the loop is running not 2 times but 3 times:
for %a in (IMG_*.*) do call :next %a
call :next IMG_0002.JPG
set newname=IMG_0002.JPG
set newname=IMG_10002.JPG
ren IMG_0002.JPG IMG_10002.JPG
call :next IMG_0013.JPG
set newname=IMG_0013.JPG
set newname=IMG_10013.JPG
ren IMG_0013.JPG IMG_10013.JPG
call :next IMG_10002.JPG
set newname=IMG_10002.JPG
set newname=IMG_110002.JPG
ren IMG_10002.JPG IMG_110002.JPG
pause
Leaving me with these 2 file names:
IMG_110002.JPG
IMG_10013.JPG
Any idea what's going on and how to fix the script?
Thank you