Backing up Files with DOS Batch Files
d. bodnar  8-26-09

 

 

A tried and true method of doing file backups involves using DOS batch files, the DOS XCOPY command and Windows Scheduler.

This method has been used on my primary computer for a number of years.  It works well with Windows XP and should work with Vista as well.

 

XCOPY

The DOS XCOPY command will copy files based on a number of command line arguments.

xcopy c:\documents e:\documents /s/e/v  

will copy the folder "documents" from the C drive to the same directory on the E drive.  It will also copy the sub directories under "documents"

xcopy c:\documents e:\documents /s/e/v  >>  edrivelog.txt  (note the space before and after ">>")

will copy the same files and create a log file of what is copied titled "edrivelog.txt"

 

BATCH files

Batch files are DOS scripts that perform a series of steps in the order that they are listed.  The batch file below can be created in NOTEPAD and saved with a ".bat" entension

time /t > G-log.txt
xcopy c:\shared_stuff G:\shared_stuff /s/e/v/r/c/a/y/d >> G-log.txt
xcopy "C:\Documents and Settings\dave\Documents" G:\Documents /s/e/v/r/c/a/y/d >> G-log.txt
xcopy C:\Current_web_dgb G:\Current_web_dgb /s/e/v/r/c/a/y/d >> G-log.txt

time /t >>G-log.txt

This batch file does the following:

  1. the first line copies the time of day (ie 9:32:44) to a file called "G-log.txt"
  2. the second line copies the contents of "c:\shared_stuff" to the same directory on drive "G" and appends (>>) the results to the "G-log.txt" file - the arguments after xcopy tell it to only copy files that have changed since the last backup and has the copy continue if there is an error - for a list of xcopy options click here
  3. the third line does the same with "documents" - note that the file name contains spaces so the file name is in quotes (")
  4. the next line copies the directory "Current_web_dgb"
  5. the last line puts the time at the end of the file.  The first and last lines let you compute how long it takes to do the copy.

I would strongly suggest that you run each line of the batch file manually from a DOS prompt before running the batch file automatically.  There is a good reason for this.  The first time you run each xcopy line you are likely to be asked if the target is a directory or a file - you need to press "D" to indicate directory - if you run the batch file the program will direct all screen output to the log file and you will never see it to respond.  The batch will hang up - if this does happen you can click on the DOS window and press D a few times to answer the question "blind"

Putting it all together

I create the batch file (edit it as needed for your use) and save it from NOTEPAD (don't use Word or WordPad as they don't create a clean text file unless you force them to).

The file is saved to the root of C but it could go anywhere.  Make sure its name has the extension ".bat"

Start Windows Scheduler (Start / Programs / Accessories / System Tools / Scheduler)

Start a new scheduled task that points to the batch file saved above.  Have it run once each day at whatever time is convenient.

I check the log files each morning to make sure that things that I worked on the day before were backed up.