Cool Engineering

Info on some cool engineering projects

Wednesday, September 12, 2007

Batch script: date/timestamped backup

One of the early batch scripts I wrote was to backup whole folders. This was particularly used during my Computer Science Honours research - where I wanted to ensure that multiple regular backups were made - particularly of documents and reports.
The script extracts time and date data from the 'Date /t' and 'Time /t' DOS commands, rearranges the order to have a date/time stamped folder that can be sequentially ordered, and then copies everything of a particular directory or directory into this new directory. Using the command line functions on WinRAR similar functionality is possible, with compression and fewer lines of code. WinRAR isn't free (although it continues to let you run it after the trial has expired), but provides good command line support. Since I can't currently find the WinRAR batch files I wrote I have provided the standard batch file below:


@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C)
@For /F "tokens=1,2,3 delims=: " %%A in ('Time /t') do @(
Set Hour=%%A
Set Minute=%%B
Set TOD=%%C)
SET DateStamp=%YEAR%-%Month%-%Day%
SET TimeStamp=%Hour%.%Minute% %TOD%
cd\
cd backup
mkdir "Backup(%DateStamp%- %TimeStamp%)"
xcopy C:\Checkout\Project\*.* c:\backup\"Backup(%DateStamp%- %TimeStamp%)" /E


An example folder created by this script was: "Backup(2007-09-12 - 09.59 PM)"

0 Comments:

Post a Comment

<< Home