Link to home
Start Free TrialLog in
Avatar of isaacr25
isaacr25

asked on

Script for renaming files

I have a .bat file with code in the code snippet attached. If the files to be renamed are already in a format such as 1.jpg, 3.jpg etc., the script skips some numbers in the sequence. How can I edit the code so that the name of the existing files do not matter?

When I run the script, I get a few of these errors:
A duplicate file name exists, or the file cannot be found.

Thanks in advance.


@echo off
 
set /a cnt=1
 
for %%a in (*.jpg) do call :PROCESS "%%a"
 
goto :EOF
 
:PROCESS
 
REM ** Remove echo to actually rename files
ren %1 %cnt%.jpg
 
set /a cnt+=1

Open in new window

Avatar of sirbounty
sirbounty
Flag of United States of America image

Not sure I understand what your goal is here.
You have files 1.jpg, 3.jpg, etc - you want 'all' jpg files to be sequentially named? 1.jpg, 2.jpg, 3.jpg, etc?
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
stick a:

if EXIST %cnt%.jpg set /a cnt+=1

before the ren

maybe check multiple times or stick it into a loop until you find a free filename