1
votes

I always have taken many photos that need to rename that with desired prefix and number, say I have the following photos

IMG_1933.JPG
IMG_1934.JPG
IMG_1935.JPG
IMG_1936.JPG
IMG_1937.JPG

Then I want to run a batch file to change them to:

Goodlooking 0098.jpg
Goodlooking 0099.jpg
Goodlooking 0100.jpg
Goodlooking 0101.jpg
Goodlooking 0102.jpg

I have read some answers here but cannot keep the leading zero while keeping the same number of digits when the number goes from 99 to 100, or 999 to 1000. Like the following:

@echo off  
pushd %~dp0  
setlocal EnableDelayedExpansion  

set filename=Goodlooking 00  
set Num=98  
for /r %%i in (*.jpg) do (  
    ren "%%i" "%filename%!Num!.jpg"  
    set /a Num+=1  
)  

The above codes can do most of the works, but cannot keep the number of digits.

That means I need to setup two variables, 1) the prefix; 2) the starting number, then keep the same number of digits.

Thank you very much.

1

1 Answers

0
votes

How about this:

@echo off  
pushd %~dp0  
setlocal EnableDelayedExpansion  

set "filename=Goodlooking"

set Num=10098
for /r %%i in (*.jpg) do (
    ren "%%~i" "%filename% !Num:~1!%%~xi"
    set /a Num+=1
)

We need to consider octal values, therefore we add 1 to the num then simply get rid of it when renaming.

to understand the problem with octal values you can open cmd.exe and run this to see the result:

set /a num=0098