Automatic Backup - No overwrite

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
douglasrac
Posts: 3
Joined: 14 Dec 2011 08:12

Automatic Backup - No overwrite

#1 Post by douglasrac » 14 Dec 2011 08:20

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?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Automatic Backup - No overwrite

#2 Post by dbenham » 14 Dec 2011 14:48

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

douglasrac
Posts: 3
Joined: 14 Dec 2011 08:12

Re: Automatic Backup - No overwrite

#3 Post by douglasrac » 14 Dec 2011 15:40

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Automatic Backup - No overwrite

#4 Post by dbenham » 14 Dec 2011 17:07

When using FOR /F, use 'command' to parse the output of a command, use "string" to parse a string value.

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

douglasrac
Posts: 3
Joined: 14 Dec 2011 08:12

Re: Automatic Backup - No overwrite

#5 Post by douglasrac » 14 Dec 2011 18:52

Thank you. It works. 8)

Post Reply