# Writing a batch file for sequential renaming



## Dalz (Jun 23, 2004)

Hi all, 

I'm new to the forums, but this looked like a good place to ask for some help on a batch file I'm trying to put together.

Here's the situation:

I'm using a beta program that generates three error log files whenever it crashes. Each time a crash occurs, it overwrites the previous log files. What I'd like to do is run a batch file after every crash that renames these files sequentially, checking for previously renamed files so it can add to the numbering from there.

For example, one of the error logs generated is called message.txt. The first time I run the batch file I want message.txt to be renamed crash1.txt. The next time the program crashes, I'll run the batch file again and have it rename the new message.txt file to crash2.txt. Rinse and repeat.

The problem I'm running in to is that I don't know how to have the batch check for the highest numbered crash?.txt file so that it can increment the next one by one. Right now, the variable is only resident per batch run, so it starts over again at 1 whenever I run it.

Here's what I'm trying to do conceptually:

If exist crash?.txt
Ren message.txt crash(?max+1).txt
Else
Ren message.txt crash1.txt


Hopefully this all makes sense. Ideally, the batch file will rename all three files at once, checking for the highest numbered existing file and then adding one.

Thanks in advance for any suggestions or thoughts on this. Oh, I'm running winxp pro, so the batch file will be set up as a simple shortcut on the desktop.

Dalz


----------



## plejon (Jul 26, 2001)

I can't really think of a way to do what you want, but maybe I can suggest this approach: instead of creating a sequential number for the files, rename the file using the date and time.

ren file.txt file-%date%-%time%.txt

I had some issues with the date not being displayed in the exact format I wanted, but you can use substrings and other techniques to get exactly what you want 

E.g. : ren file.txt file%date:~3,10%-%time%.txt

For more info on the %date% and %time% variables and substringing, just open a Command Prompt box and type set /?


----------



## Dalz (Jun 23, 2004)

Thanks plejon. I eventually ended up going that route since I couldn't figure out how to do the sequential renaming. I've had some odd issues with it working sometimes and not others, though, with no changes to the batch file. For the times when it doesn't work, when it gets to a rename step it gives me an 'incorrect syntax' error. Here's what I've got (credit and thanks go to billious) -- do you see anything that might cause that?

path=%path%

move message.txt c:\errordump
ren c:\errordump\message.txt %date:~4,2%%date:~7,2%%date:~12%_%time:~0,2%%time:~3,2%%time:~6,2%.txt

move site.err c:\errordump
ren c:\errordump\site.err %date:~4,2%%date:~7,2%%date:~12%_%time:~0,2%%time:~3,2%%time:~6,2%.err

move sitestk.dmp c:\errordump
ren c:\errordump\sitestk.dmp %date:~4,2%%date:~7,2%%date:~12%_%time:~0,2%%time:~3,2%%time:~6,2%.dmp

for /f "tokens=*" %%i in ('dir *.dwg /o:d/a-d/b') do set filename=%%i

copy %filename% c:\errordump


Thanks!

Dalz


----------



## plejon (Jul 26, 2001)

I don't see an error immediately. try to find a pattern. When do you get the error ? When not ? What's the difference between the two situations


----------



## guard (Jun 18, 2004)

Dalz said:


> Thanks plejon. I eventually ended up going that route since I couldn't figure out how to do the sequential renaming. I've had some odd issues with it working sometimes and not others, though, with no changes to the batch file. For the times when it doesn't work, when it gets to a rename step it gives me an 'incorrect syntax' error. Here's what I've got (credit and thanks go to billious) -- do you see anything that might cause that?
> 
> path=%path%
> 
> ...


Here's a few suggestions that will make your script easier to read and maintain while adding reliability. The code below uses the FREE Advanced NT/2K/XP/K3 Command Library from TheSystemGuard.com.

For the benefit of others who may be reading, I added cross-platform compatibility with NT4, 2000, XP and Server 2003.

All commands are color coded to help in understanding.

Windows internal commands are *Dark Red* and are linked to a "Mounted Help" page. This is a merged help page showing the differences among NT, 2K, XP and K3.

"Mount/\Commands" are *Navy* and are linked to their respective home page.

#Constants and other environment variables are Green.

*******

@*ECHO* OFF

*SETLOCAL* ENABLEEXTENSIONS

*CALL* ntlib.cmd /Init /Quiet

*CD* /D _{YourSourceDirectory}_

*FOR* *%%i* IN (message.txt site.err sitestk.dmp) DO (
*%.IfNT%* (
*MOVE* "*%%i*" "c:\errordump"

*REN* "c:\errordump\*%%~nxi*" "%#LogDate%-%#LogTime%*%%~xi*"​) ELSE (
*MOVE* "*%%i*" "c:\errordump\%#LogDate%-%#LogTime%*%%~xi*"​)​)

*%.ForAll%* *%%j* IN ('*DIR* *.dwg /o:d /a-d /b') DO (*SET* "LatestFile=*%%j*"​)

*COPY* "%LatestFile%" c:\errordump

*******

Notes:

The Command Library provides over 200 resources to write self-documenting and reliable shell scripts (batch files). Each resource is constructed using only what comes with Windows and everything is contained within a single *.cmd* file. There are no third-party executable files.

The first *FOR* loop (*%%i*) runs through the three files and moves each one to the c:\errordump folder. Once there, the files are renamed to yyyymmdd-hhmmss.ext​ where ".ext" is replaced by the file extension.

The *.ifNT* Mt/\Cmd is used because the internal *MOVE* command supports simultaneous renaming only in Windows 2000 and above. So NT4 will need to do it in two steps (*MOVE* and *REN*).

The second *FOR* loop (*%%j*) uses the *.ForAll *  Mt/\Cmd (same syntax as *FOR*, see the link above). *%.ForAll%* is just a shorter self-documenting way of saying *FOR* /F "tokens=* delims="​The #CONSTANTS called #LogDate  and #LogTime  are updated when the library is initialized and are always *yyyymmdd* and *hhmmss*, regardless of the regional date and time settings. If your script was longer, and you wanted to update these values immediately before using them, the *%.GetLogDate%* and *%.GetLogTime%* commands would be used.

Anything that is (or could be) a filename should be enclosed in "" just in case there are embedded spaces or other characters that are OK in filenames but unreliable in shell scripts.

Changing to your source directory using "*CD* /D" is always a good idea just in case the script is not started from there.

The script assumes that "ntlib.cmd" is your path but if it's not you can always call the library from somewhere else using *drive:\path\ntlib.cmd* or *\\server\share\ntlib.cmd*.


----------



## Dalz (Jun 23, 2004)

Thanks for your help guys -- I've had to put this on the backburner for a bit, but should be getting back to it shortly. I'll try and get the error message to repro (I can't seem to get it to happen on command), and I'll also give your code a shot, guard. Both of you have been very helpful.


----------

