HOWTO: Backup using a batch script
[ 18 August 2006 ]
Marked under time savers, windows.

So one day at work I wrote a batch script to do a daily backup of a directory for a website I was working on. I had a directory on my local machine with the HTML files. I got tired of copying the files and renaming the directory to the date. So, my intent was to create a way to copy that directory to a folder named by the current date on a network share using a script.

Here’s the full script:

for /f "tokens=2-4 delims=/ " %%g in ('date /t') do (
  set mm=%%g
  set dd=%%h
  set yy=%%i
)


if exist "\\path\to\network\folder\Daily Backup\%mm%-%dd%-%yy%" (
  rd /S /Q "\\path\to\network\folder\Daily Backup\%mm%-%dd%-%yy%"
)

xcopy "C:\Documents and Settings\Administrator\Desktop\website" "\\path\to\network\folder\Daily Backup\%mm%-%dd%-%yy%" /s /i


And now for an explanation of how each piece works:


for /f "tokens=2-4 delims=/ " %%g in ('date /t') do (
  set mm=%%g
  set dd=%%h
   set yy=%%i
)

This for loop will execute the command date /t and pull the result pieces 2 through 4 into variables. %%g means the first variable will be g, followed by h and i. Then, those results stored in %%g, %%h, and %%i are placed into mm, dd, and yy.


if exist "\\path\to\network\folder\Daily Backup\%mm%-%dd%-%yy%" (
  rd /S /Q "\\path\to\network\folder\Daily Backup\%mm%-%dd%-%yy%"
)

This condition will check to see if the folder for today’s backup exists. If so, that mean’s you’ve already run the backup and we should discard it.


xcopy "C:\Documents and Settings\Administrator\Desktop\website" "\\path\to\network\folder\Daily Backup\%mm%-%dd%-%yy%" /s /i

This last line does the work. It uses xcopy to copy the first directory to the second. Notice the first is your local machine and the second is the network share where the variables %mm%, %dd%, and %yy% are used in the path. These are replaced with the actual values from part one.

And that’s it. Now you can backup a directory for each day and have a clean, organized repository.


17 Comment’s on “HOWTO: Backup using a batch script”

Igor R says:

You are the best !!!
It helped me very well.
I was trying to fing solution for my problem about 4 days.

Graeme M says:

Excellent, took the lazy route of surfing the net looking for batch commands to handle dates and google located this page !!

Thomas W says:

Ever tried:
(Date format dd.mm.yyyy in my case)
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%

(Date format yyyy-mm-dd in your case)
set yy=%date:~-10,4%
set mm=%date:~-5,2%
set dd=%date:~-2%

Vikas Kale says:

Date format dd.mm.yyyy in my case)
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%

(Date format yyyy-mm-dd in your case)
set yy=%date:~-10,4%
set mm=%date:~-5,2%
set dd=%date:~-2%

Will not work on Windows 98 SE environment. How it could be????

udomchok says:

how can i display yy in 2 digit?

Santosh says:

Great

acisco says:

The destination directory is named “–” not as the date, only the dividers (-).
I’m running on a 2003 Windows Server.

I also need the directory to be named with time, and take as many copies I want in one day. So in fact the batch does not need to check for existing dir/files.

Arpan says:

Very well explained. Thanks.

Jason Gurtz says:

Excellent posting, thank you.

I had almost given up and was about to go powershell.

Aatif-Ali says:

Excellent Work. But it doesn’t check the existing folder and overwrite the last taken backup. Any idea why it can’t check the existing folder.

Aatif-Ali says:

Okay now I understand that rd /s /q switch does this. So I have to remove that one. Working on it will tell you the solution.

arvind says:

It worked in Win XP, but I faced the problem with Win 98 SE, looks like it is throwing exception with the for /f …..

Can anybody suggest me what should be the problem win Win 98 SE ????

Jiri says:

Hi,

On W2K3 just change tokens=1-3

j2 says:

I love you.

PHLAK says:

“how can i display yy in 2 digit?” -udomchok

Haven’t you learned anything from Y2K?

rain says:

thanks to help me out…

Graham says:

I did a version for windows 2003 server using date and time and for dd/mm/yyyy.

I only used it to create a folder so it can backup automatically

Here’s the code, not pretty but works:

for /f “tokens=1-3 delims=/ ” %%g in (’date /t’) do (
set mm=%%h
set dd=%%g
set yy=%%i
)
for /f “tokens=1-2 delims=: ” %%j in (’time /t’) do (
set hh=%%j
set mn=%%k
)

md “m:\%dd%-%mm%-%yy%\%hh%h%mn%m”

Speak your mind:

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>









spam, email at: spamstudygonewild+atm@gmail.com