1.

Solve : Using an Incrementing variable in a For /R Loop?

Answer»

I have a number of log files that all get named the same when created, but placed in different directories. I need to move and rename all the files to one directory, and have come up with a way to name them according to the computer name and time/date of move, but the batch executes so quickly that all the files end up with the same name still.

I figure the best way to eliminate the problem is to use a incremented variable to add to the name (blahFile_1.csv,blahFile_2.csv,blahFile_3.csv) This is the code I have so far:

Code: [Select]@setlocal enableextensions
@cd /d "%~dp0"
@echo off
SETLOCAL enabledelayedexpansion enableextensions


cd ..\..\..\Accounts\logs\FileUsage\


for /R %%A in (*FileUsageSummary*.*) do (


set fCount=1
copy "%%A" D:\Logs\FileUsage\%computerName%_%time:~0,2%%time:~3,2%_%date:~4,2%%date:~7,2%%date:~-2,2%_!fCount!.csv
set fCount+=1
echo !fCount!

)
ENDLOCAL

My output files however, still end up named the same. What am I doing wrong?

THANKS!
-ian
You need to use !date:~2,2! with the exclamation marks with delayed expansion. Similar with the time.

And your set command NEEDS a /a

set /a fcount+=1

and put this at the top of the batch file before the forindo loop:

set fCount=1

One thing to consider is USING a format similar to Filename-YYYYMMDD-HHMMSS.ext to name your files as this will sort naturally in a folder.

You can also add this in your loop and it will delay each file copy slightly so that each has a distinct time stamp.
ping -n 3 localhost >nulThat worked perfectly! Thank you for the help!

Why did I need the /a? I suspect that's the major missing part. What does that tell DOS to do with the variable?/a tells it it is to do arithmatic otherwise you are SETTING the string 'fCount+' equal to 1 and not adding 1.Quote from: dustmites on August 07, 2012, 01:33:13 PM

Why did I need the /a? I suspect that's the major missing part. What does that tell DOS to do with the variable?

1. a (or A) is for Arithmetic. To find all the things that SET can do, type SET /? at the prompt
2. This isn't DOS.



Discussion

No Comment Found