.bat for batch rename to increment numbers in fname

werdnanoslen picture werdnanoslen · Jun 12, 2011 · Viewed 24.2k times · Source

I have a large folder of .cbr's, and I'm renaming them by issue number to correctly order them. What do I need to include in the ren line to have each file increment the number in the file name via windows command prompt? I'll be doing this frequently so I'll make this a .bat file.

For example, where n = initial number and m = final number: n.cbr, (n+1).cbr, ..., (m-1).cbr, m.cbr

The .bat thusfar:

ren *.cbz *.cbr
ren *.cbr <increment numbers n through m>.cbr

Alternatively, how do I trim each file name so that only the numbers are left before the extension? (from issue1.cbr to 1.cbr) via either a .bat or script host file?

Answer

PA. picture PA. · Jun 12, 2011

Try this batch script.

@echo off
setlocal enabledelayedexpansion
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.cbr') do (
 echo ren "%%a" !count!.cbr
 set /a count+=1
)

It renames all the files with a incremental counter. The order of the files is preserved with the /OD option of the DIR command, that sorts the files list by its modified timestamp.

After careful testing, remove the ECHO command.

For more information, read HELP DIR, HELP SET and HELP FOR.