1.

Solve : Batch file to copy and Increment?

Answer»
After wasting far too much time trying to figure this out I thought I'd just ask.

I'm trying to write a batch file (OS is WInXP) that will copy (or use xcopy) one .txt file from 1 directory into another directory on a different drive and then increment the file name so I have several backups of the original file.

This is simple DOS stuff but I'm stumped never having WRITTEN batch files.


Any help would be greatly appreciated.

David....This will date and time stamp a file. That's much more useful than a plain number.

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

Code: [Select]@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

copy "c:\folder A\file.txt" "c:\folder B\file-%fullstamp%.txt" >nul

pauseset destination=C:\Dummy\
set Location=D:\Dummy\Test.txt
set FILENAME=Test
set a=1

:LOOP
if exist %destination%\%filename%_%a% set /a a+=1 && goto :loop
copy %location% %destination%\%filename%_%a%.txt

Untested
Though what foxidrive say's is true, a date\time would be much better for you.Thank you both for your efforts. I will most likely use the one that adds the date stamp but I also like the one that adds -1 to the file name.

Is there a string I can add to that batch file where it will increment file name (filename-1.txt, filename-2.txt, -3.....etc)?
Not in one command, I CREATED a loop that would increment a variable (%a%) by 1 each time it ran, then had it copy the file X times saving it with "-%a%" at the end. This would be the way I would do it, but I'm sure there are may other ways to complete the task.


Discussion

No Comment Found