Counters with FOR in Batch Files

HP
September 7, 2007 at 23:13:29
Specs: XP Home, 2.2GHz/1Gb

I'm trying to use a batch job to rename a whole bunch of files with sequential numbering based on date order. I'm currently using a FOR loop and trying to use a counter in the loop, but without success. My batch file just attempts to rename each file with the same number. Any suggestions?

DIR /B -D *.JPG > DIR.LST
SET /A COUNT=0

FOR /F %%a IN (DIR.LST) DO (
SET /A COUNT=%COUNT%+1
ECHO %COUNT%
REN %%a CIMG_%COUNT%.JPG
)


See More: Counters with FOR in Batch Files

Report •


#1
September 8, 2007 at 01:19:48

:: append seq num to JPGs

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d/od *.jpg') do (
set /a COUNT+=1
REN %%a CIMG_!COUNT!.JPG
)
goto :eof

notes:

[1] If by -D you want to exclude directories, use /a-d
[2] If you want reverse date order, use /o-d
[3] No need to initialize COUNT, unless you want to start at 0; this starts at 1.
[4] If run more than once, this will 'double append' the COUNT. If you need
a second run to include files added, you can use additional code to make it
'take up where it left off'.


=====================================
If at first you don't succeed, you're about average.

M2



Report •
Related Solutions


Ask Question