I have a Windows batch script that renames files in a folder sequentially(zero padding with 4 digits) with a constant name at the start and end of output filenames.
For eg:- If a folder contains 40 jpeg images i could do myscript.bat *.jpg image .jpg
to rename the jpeg files as image0001.jpg, image0002.jpg,...,image0040.jpg
myscript.bat
@echo off
setlocal EnableDelayedExpansion
set /A i=1
for %%G in (%1) do (
set padded_i=00000!i!
set newName=%2!padded_i:~-4!%3
rename "%%G" "!newName!"
set /A i=1+!i!
)
echo "Loop run !i! times"
But the problem is that when I tested the script it is found that sometimes the first file get renamed twice(i.e the file that is renamed to 0001 first gets renamed again to 0041 after all other files have been renamed), So that the renamed 40 jpeg files look like image0002.jpg, image0003.jpg,...,image0041.jpg
On furthur checking I found the following result:-
#myscript.bat *.jpg images .jpg
"Loop run 40 times"
#myscript.bat *.jpg images_ .jpg
"Loop run 41 times"
#myscript.bat *.jpg images_1 .jpg
"Loop run 41 times"
#myscript.bat *.jpg images_ .jpg
"Loop run 40 times"
#myscript.bat *.jpg images_2 .jpg
"Loop run 41 times"
#myscript.bat *.jpg images_ .jpg
"Loop run 40 times"
#myscript.bat *.jpg imagess .jpg
"Loop run 40 times"
#myscript.bat *.jpg imagesw .jpg
"Loop run 40 times"
#myscript.bat *.jpg imbgesw .jpg
"Loop run 41 times"
#myscript.bat *.jpg imbgfsw .jpg
"Loop run 41 times"
As you can see that when a new character is added the script loops 41 times and if the character at the end is removed or replaced by another there is no problem but if a character in the middle is replaced it loops over 41 times.
Any help or information is aprreciated.