Oh, boy! It's XCopy!
Using XCopy for backups, including the tricky "Exclude" command and writing to log files

XCopy is a little application built in to Windows XP. Combine it with Scheduled Tasks for a free and semi-easy way to do automated scheduled backups.

I use it by opening a new Notepad file and typing in commands, then saving it as a batch file - that is, save as "mybatchfile.bat" to make the file executable. Then I use Windows' built-in scheduler (under "Scheduled Tasks" in the Control Panels) to set it to run automatically.

Here are some sample commands I use in the .bat file and explanations of what they do. I'm using color coding to clue you as to which command I'm explaining - otherwise the colors don't have any meaning and you shouldn't try to use them in your .bat file.

What I am doing with the colorful xcopy line below is backing up a folder and all its subfolders to another drive. I don't want it to back up the same files over and over, I only want them backed up if they are not already backed up. If a file has been backed up already but there is a newer version, I want to overwrite the old version automatically. I also want to exclude some files that are in the directory being backed up.

Example:

xcopy "c:\Documents and Settings\User1\My Documents" "F:\My Documents" /d /e /y /EXCLUDE:C:\exclude.txt

This invokes the xcopy application and has to be at the beginning of each line.

This is the path to the directory I want to back up (the Source). Note it is in quotes and doesn't have a trailing slash.

This is the path to the place I want the backup to go (the Destination). In this case, I'm copying from the C drive to the F drive. Note that it is in quotes and doesn't have a trailing slash.

/d is a switch - it tells xcopy to copy all the Source files that are newer than existing Destination files. This allows you to update files that have changed.

/e is a switch - it tells xcopy to copy all subdirectories, even if they are empty. So, if my user has a bunch of folders inside her My Documents folder, they will be copied, too.

/y is another switch. It stops xcopy from asking "file already exists, are you sure you want to copy over it?" when it finds a newer version of a file it has already backed up.

Exclude:

/EXCLUDE:C:\exclude.txt is tricky to use so I will break it down for you. /EXCLUDE: tells xcopy that you want to exclude some files. You can't just type a list of things to exclude in the batch file. You have to make a new text file and put a list of files, file types, or directories to exclude in that file. Enter each item you want to exclude on its own line. Then, in your batch file you point to that "exclude" text file so xcopy can find it and read it. In my case, I just wanted to NOT back up the user's music files, which were all in the \My Documents\My Music\ directory. So, I made a new notepad file and called it exclude.txt - in the file is only one line:

\My Music\

Note that it is enclosed in slashes - this indicates a directory. If I had wanted to exclude (for instance) .mp3 files regardless of the folder they were in, I would have a line with just .mp3 on it. You don't have to have a direct path to the item(s) to be excluded in the exclude file, just the name of the folder or file or file type will work. But in the batch file, you do have to specify a direct path to the exclude text file. IMPORTANT: The path to the exclude file MAY NOT HAVE ANY SPACES IN IT. This is a critical restriction that will cause your Exclude command to fail if you don't heed it. Thus, C:\exclude.txt - I simply put the exclude file at the root of my C drive to satisfy the restriction on spaces in the path.

Here are a couple more lines to give you some examples. They work the same way as the one I've deconstructed above. Note that the destination name doesn't have to match the source name. You can call the destination whatever you want.

xcopy "c:\Documents and Settings\Jeff\Desktop" "F:\Desktop" /D /E /Y /EXCLUDE:C:\exclude.txt

xcopy "C:\Documents and Settings\Jeff\Application Data\Qualcomm\Eudora" "F:\Email" /D /E /Y

A complete backup file:

Here is a complete batch file that backs up the My Documents folder to the G: drive. When it runs, it opens the Command window, the "echo" commands below tell it to show that text in the Command window. I am using >> to send "Backup Started," then the date, time, a list of what was backed up, "Backup finished," and a separator line to easily visually distinguish consecutive backups to a log file called "backup.log." Enjoy!

echo Backup Started >> backup.log
date /T >> backup.log
time /T >> backup.log
xcopy /D "C:\Documents and Settings\me\My Documents" "G:\My Documents\" /e /y /EXCLUDE:C:\exclude.txt >> backup.log
echo Backup finished >> backup.log
echo ----------------------------------------------------------------------------- >> backup.log


I was so pleased at finally getting "EXCLUDE" to work that it inspired me to write this page up. I made it as a reference for myself, but perhaps it will be useful for you, too. If you want more info and lots more switches and options for xcopy, check here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx - it's confusing in some ways, particularly the section on EXCLUDE. I think mine is better.

Questions or comments? Email me!


more of my tech support documents

home