Increment filenames?

January 7, 2010 at 12:33:08
Specs: Windows XP
I have a batch file that I would like to log. I don't want the log to be of infinite length, but I also don't want it to be deleted every time.

Here's what I was thinking:

del log9.txt /q
for /l %%i in (8,-1,0) do set /a j=%%i+1 && 
ren log%%i.txt log%j%.txt

But it doesn't work. J shows up empty. If I change the set command to

set /a j=i+1
J isn't empty, but it doesn't change either.

With only 9 files to change, I could just do commands one-by-one, but I'd really rather my for loop work.


See More: Increment filenames?

Report •

January 7, 2010 at 13:13:22
setlocal enabledelayedexpansion
for /l %%i in (8,-1,0) do set /a j=%%i+1 && move /y log%%i.txt log!j!.txt

(move /y is usually preferable to ren because ren won't over-write a file. delayedexpansion makes the %j% accessible inside the for-loop)

Report •
Related Solutions

Ask Question