Solved DOS batch multiple file rename

September 23, 2011 at 06:23:42
Specs: Windows XP
Hi!

How can I rename all files to numbers:

folder\photohttr.jpg
folder\photosfsdf.jpg
folder\photomkmk.jpg

to

folder\1.jpg
folder\2.jpg
folder\3.jpg

And there is lot of jpg that I need to rename like this, so I need command to rename whole folder of jpg files.

Thanks


See More: DOS batch multiple file rename

Report •


#1
September 23, 2011 at 10:12:37
This isn't tested, but it should be close to what you need.

@echo off

REM using dir to obtain a list of the .jpg file names in c:\folder
dir /b c:\folder\*.jpg > filelist.tmp

REM declaring a variable that will be incremented to create the numbered
REM file names you wanted
set numprefix=0
REM looping through the file list we created earlier
for /F "tokens=*" %%A in (filelist.tmp) do (
REM adding 1 to numprefix variable to create next number filename
set /a numprefix=%numprefix% + 1
REM renaming original file to be current value of numprefix variable .jpg
ren c:\folder\%%A %numprefix%.jpg
)
REM removing the temporary file list file
del /q /f filelist.tmp


Report •

#2
September 23, 2011 at 12:35:55
Well it renames only one or first file but not all files...

Edit:

I found it! Its going like this:

x1.bat
=====
@echo off
set /a counter=0
for %%f in (*.jpg) do call x2.bat "%%f"

x2.bat
=====
@echo off
set /a counter=%counter%+1
ren "%1" file%counter%.jpg


Report •

#3
September 23, 2011 at 13:46:16
✔ Best Answer
Oops... forgot to enable delayed expansion. Try this one. I tested it this time, and it works.


@echo on
SETLOCAL ENABLEDELAYEDEXPANSION
REM using dir to obtain a list of the .jpg file names in c:\folder
dir /b c:\folder\*.jpg > filelist.tmp
REM declaring a variable that will be incremented to create the numbered
REM file names you wanted
set numprefix=0
REM looping through the file list we created earlier
for /F "tokens=*" %%A in (filelist.tmp) do (
REM adding 1 to numprefix variable to create next number filename
set /a numprefix=!numprefix!+1
REM renaming original file to be current value of numprefix variable .jpg
ren "c:\folder\%%A" !numprefix!.jpg
)
REM removing the temporary file list file
del /q /f filelist.tmp
ENDLOCAL


Report •
Related Solutions


Ask Question