Hi,
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?
Automatic Backup - No overwrite
Moderator: DosItHelp
Re: Automatic Backup - No overwrite
I can think of an easier solution - include the time in the folder names. Use the time that your backup process starts.
You can use %TIME% to get the time value. You will have to replace : with some other character because it cannot be used in a folder or file name.
In a similar fashion you can use %DATE% instead of DATE/T.
Dave Benham
You can use %TIME% to get the time value. You will have to replace : with some other character because it cannot be used in a folder or file name.
In a similar fashion you can use %DATE% instead of DATE/T.
Dave Benham
-
- Posts: 3
- Joined: 14 Dec 2011 08:12
Re: Automatic Backup - No overwrite
Thanks for the reply.
I don't see how can I put time in the folder with %DATE% instead of DATE/T.
For me it can be anything, just to make it different and avoid overwrite.
I couldn't use %TIME% because I don't know how to construct the code, since it will change the delimiter.
I don't see how can I put time in the folder with %DATE% instead of DATE/T.
For me it can be anything, just to make it different and avoid overwrite.
I couldn't use %TIME% because I don't know how to construct the code, since it will change the delimiter.
Re: Automatic Backup - No overwrite
When using FOR /F, use 'command' to parse the output of a command, use "string" to parse a string value.
With the above you have everything you need to build your folder name with date and time, using whatever delimiters you choose. I chose to create a folder name using "YYYY-MM-DD hh_mm_ss.ss" format. YYYY=year, MM=month, DD=Day, hh=hours, mm=minutes, ss.ss=seconds
Dave Benham
Code: Select all
FOR /F "TOKENS=1-6 DELIMS=/: " %%A IN ("%DATE% %TIME%") DO (
echo year=%%C
echo month=%%A
echo day=%%B
echo hours=%%D
echo minutes=%%E
echo seconds=%%F
set "folder=%%C-%%A-%%B %%D_%%E_%%F"
)
With the above you have everything you need to build your folder name with date and time, using whatever delimiters you choose. I chose to create a folder name using "YYYY-MM-DD hh_mm_ss.ss" format. YYYY=year, MM=month, DD=Day, hh=hours, mm=minutes, ss.ss=seconds
Dave Benham
-
- Posts: 3
- Joined: 14 Dec 2011 08:12
Re: Automatic Backup - No overwrite
Thank you. It works. 
