Make Backup of specific files with sequential filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ttcg52
Posts: 2
Joined: 05 Mar 2015 23:07

Make Backup of specific files with sequential filename

#1 Post by ttcg52 » 05 Mar 2015 23:21

I want to make a backup of a certain file manually each time i open a bat file, but I don't want to overwrite the file each time it's backed up, instead I want to create several copies of the file and save it with a sequential file name. That's because the file changes very frequently.

Example:
MyFile (1).txt
MyFile (2).txt
MyFile (3).txt
MyFile (4).txt

Or without brackets would be better, anyway I don't mind brackets or anything I just don't want to overwrite the file and create new copies each time it's backed up.

Another way that I thought about doing this is by putting the file into sequential folders, that'll be okay too.

Anyways, I am pretty sure this is very easy I just need the code I am not being able to find anything from google.

Thanks everyone!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Make Backup of specific files with sequential filename

#2 Post by foxidrive » 06 Mar 2015 00:50

Adding a date and time in a format that sorts correctly is a better way to handle this.

ttcg52
Posts: 2
Joined: 05 Mar 2015 23:07

Re: Make Backup of specific files with sequential filename

#3 Post by ttcg52 » 06 Mar 2015 02:40

foxidrive wrote:Adding a date and time in a format that sorts correctly is a better way to handle this.

can you provide a code? i am not that good at code...

i have found this on google but its not exactly what i want and it doesnt work:

Code: Select all

@echo off
:: variables
set backupdir="%USERPROFILE%\Desktop\folder1"
set backupcmd=xcopy "%USERPROFILE%\Desktop\folder2\file.txt"

echo ### Backing Up Saves...
xcopy /s "%USERPROFILE%\Desktop\folder1\file.txt" "%backupdir%/%time%"

echo Backup Complete!
@pause



can anyone please correct that code so it does what i described in the first post ?? thanks!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Make Backup of specific files with sequential filename

#4 Post by foxidrive » 06 Mar 2015 02:57

Test this:

Code: Select all

:: The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
@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 "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" 

copy "my file.txt" "c:\wherever-you-want-it\my file %datestamp%_%timestamp%.txt"

Post Reply