Counter not working

Microsoft Windows xp pro w/ sp3 multilin...
June 17, 2011 at 04:33:33
Specs: Windows XP
I'm a newby in Batch files but can't make work the counter in the following script to rename files:

setLocal EnableDelayedExpansion
Set /A Counter=0
for /r %%x in (%1) do (
ren "%%x" "myPictureNumber%Counter%.%2"
Set /A Counter=+1
echo %Counter%
Counter value is always 0
I'm trying to rename all files in a folder passing two parameters: mybatch.bat *.jpg avi (for example)

See More: Counter not working

Report •

June 17, 2011 at 05:24:38
When you enable the Delayed Expansion you must mark the environment variables by ! instead of the usual %, i.e.

setLocal EnableDelayedExpansion
set /A Counter=0
for  %%x in (%1) do (
  ren "%%x" "myPictureNumber!Counter!.%2"
  set /A Counter+=1

and use for without the /R switch to select the files in the folder since /R forces another behavior (see for /?).

Report •

June 20, 2011 at 04:34:50
Works like a charm except for one thing:
I had to change the line that increments the Counter

yours (not working):
sets /A Counter+=1

mine (working)
sets /A Counter=!Counter!+1


Report •

June 20, 2011 at 07:07:31

set /A Counter+=1 NOT sets

I can assure works perfectly as you can learn by typing set /?.

This syntax for set /A mimics C language and is highly useful in loop coding.

Report •
Related Solutions

Ask Question