1.

Solve : Rename file each time batch file runs?

Answer»

I am currenlty using a simple batch file:

@echo off
pdm_webstat.exe -d > c:\pdm_webstat.txt

I want to be able to rename the .txt file each time the batch file is run. How can I accomplish this?

Thanks you can keep a counter in a file, or you name your output file with some sequence. EG c:\pdm_webstat01.txt. so the next time you run the batch, check for that number and increase by 1.. I've tried this but i just overwrote the document. I will try your other solution. What do you mean by counter?

@echo off
pdm_webstat.exe -d > c:\pdm_webstat01.txt

QUOTE from: lsblanchard on July 06, 2007, 09:22:43 AM

What do you mean by counter?
it's just basically a file that contains the count.
by naming the files with a number, it should be more clean than having to maintain this counter file.This didn't work:

Name your output file with some sequence.

eg c:\pdm_webstat01.txtQuote from: lsblanchard on July 06, 2007, 10:01:08 AM
This didn't work:

Name your output file with some sequence.

eg c:\pdm_webstat01.txt
show what you have done.isblanchard, you need to use batch arithmetic.

study the /a switch of the SET command.

try this :
Code: [Select]@echo off
for /L %%a in (0,1,9) do for /L %%b in (0,1,9) do (
if not exist "c:\pdm_webstat%%a%%b.txt" (
pdm_webstat.exe -d > c:\pdm_webstat%%a%%b.txt
exit
)
)
Quote from: Fen_Li on July 07, 2007, 02:00:23 PM
try this :
Code: [Select]@echo off
for /L %%a in (0,1,9) do for /L %%b in (0,1,9) do (
if not exist "c:\pdm_webstat%%a%%b.txt" (
pdm_webstat.exe -d > c:\pdm_webstat%%a%%b.txt
exit
)
)
have you considered what happens every time this batch is run?@echo off

REM Two examples of variable file name


REM Output to file with date/time stamp. Will work for files that are greater than one minute apart

REM Get date and time and set to a variable
for /f "tokens=1,2 delims=: " %%a in ('time /t') do set t1=%%a%%b
for /f "tokens=2,3,4 delims=/ " %%a in ('date /t') do set t1=%%c%%a%%b%t1%

REM Write output with date/time stamp
pdm_webstat.exe -d > c:\pdm_webstat%t1%.txt


REM Output file with sequential numbering.

REM Self seed from script and maintain sequence of multiple runs
REM Sequence file pointer
SET Sequence=C:\Sequence.cmd
REM Get current sequence if it exists
REM MUST use CALL
IF exist %Sequence% call %Sequence%
REM Seed the sequence if it was not found, start at zero!
IF not Defined NumTimes SET /a NumTimes=0
REM Add one to the sequence. This is why started at zero.
SET /a NumTimes+=1
REM Store the value in a file for next time
ECHO SET NumTimes=%NumTimes%>%Sequence%

REM Output a sequenced file
pdm_webstat.exe -d > c:\pdm_webstat%NumTimes%.txt


REM NOTE that this does not clean up old files or ROTATE files.


Discussion

No Comment Found