Automatic Backup - No overwrite

December 14, 2011 at 06:26:00
Specs: Windows 7

I made a batch file that will be executed everyday at a specified time. Basically all it does it to copy some files from one folder to another and put the date in it, so I have several versions of the same file.

@echo off
FOR /F "TOKENS=1-4* DELIMS=/" %%A IN ('DATE/T') DO (
SET Year=%%C
SET Month=%%B
SET Day=%%A
FOR %%A IN (%Day%) DO SET Day=%%A
FOR %%A IN (%Month%) DO SET Month=%%A
FOR %%A IN (%Year%) DO SET Year=%%A

mkdir "C:\Users\%USERPROFILE%\Desktop\Codinome\Backups\%Day%.%Month%.%Year%"

copy "C:\Users\%USERPROFILE%\Documents\Cities In Motion\maps\Walden.grid" "C:\Users\%USERPROFILE%\Desktop\Codinome\Backups\%Day%.%Month%.%Year%\Walden.grid %Day%.%Month%.%Year%"

My problem now is that I will eventually execute this batch file by myself, when I need a backup before making any important changes to the file. If I do that, the file will be overwrite. Since I can only have 1 per day.

So I think about 2 solutions:

1) Add time to the name of the file.

2) Somehow make the copy command not overwrite any files (and also not prompt). But it will copy anyway, thus copy but rename, preferable with a sequential number.

How can I make any of those solutions?

See More: Automatic Backup - No overwrite

Report •

December 15, 2011 at 08:06:28
you could use
if exist "C:\Users\%USERPROFILE%\Desktop\Codinome\Backups\%Day%.%Month%.%Year%" goto existbackup

md "C:\Users\%USERPROFILE%\Desktop\Codinome\Backups\%Day%.%Month%.%Year%1"
copy "C:\Users\%USERPROFILE%\Documents\Cities In Motion\maps\Walden.grid" "C:\Users\%USERPROFILE%\Desktop\Codinome\Backups\%Day%.%Month%.%Year%1\Walden.grid %Day%.%Month%.%Year%"

or something like that


Report •

December 15, 2011 at 14:53:34
Thank you for the reply. But I manage to make it work doing:

set DD=%DATE:~0,2%
set MM=%DATE:~3,2%
set YY=%DATE:~8,2%
set YYYY=%DATE:~6,4%
set HH=%TIME:~0,2%
set MN=%TIME:~3,2%

And then usuing %HH% and %MN% with the copy command. It is much simpler than my previous head.

Report •

December 16, 2011 at 06:33:32
Glad it worked out. Thanks for the fast response. :-)


Report •
Related Solutions

Ask Question