Skip to content →

HOWTO: Backup using a batch script

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.

Published in time savers windows

19 Comments

  1. Igor R Igor R

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

  2. Graeme M Graeme M

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

  3. Thomas W Thomas W

    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%

  4. Vikas Kale Vikas Kale

    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????

  5. udomchok udomchok

    how can i display yy in 2 digit?

  6. Santosh Santosh

    Great

  7. 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.

  8. Arpan Arpan

    Very well explained. Thanks.

  9. Jason Gurtz Jason Gurtz

    Excellent posting, thank you.

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

  10. Aatif-Ali Aatif-Ali

    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.

  11. Aatif-Ali Aatif-Ali

    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.

  12. arvind arvind

    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 ????

  13. Jiri Jiri

    Hi,

    On W2K3 just change tokens=1-3

  14. j2 j2

    I love you.

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

    Haven’t you learned anything from Y2K?

  16. rain rain

    thanks to help me out…

  17. Graham Graham

    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”

  18. Jorge Jorge

    What about the seconds? I mean, how can I do if I want to get the seconds too?

  19. just wanted to say thankyou, microsoft removed network backups from win7 home premium, so this is a quick and simple replacement to a feature that shouldn’t be missing.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.