ROBOCOPY is an excellent command line utility that Microsoft began bundling with Windows Vista, and has since been included in Windows 7 and Server 2008 (R2 as well). It makes it incredibly simple to mirror a directory tree, and is great for batch file backups. There are a few quirks with it, however.

When using the /MIR switch, ROBOCOPY overwrites the destination path with everything in the source path. To exclude folders, you’re instructed to use the /XD switch. Below is a sample with the correct syntax:

robocopy d:\ n:\Data /MIR /Z /A-:R /R:1 /W:1 /LOG:n:\logs\data.log /XD d:\vms d:\wsus “d:\System Volume Information” d:\tmpbak d:\installs d:\RECYCLER

To break this down, we start with the ROBOCOPY command itself. Following that is the Source Path (D:) and then the Destination Path (N:Data). Next is the /MIR switch that tells ROBOCOPY to mirror the existing directory structure and copy everything. The /Z switch sets the job in Restartable Mode, and /A-:R removes any read-only attributes. /R:1 and /W:1 tell ROBOCOPY to Retry copying the file once if it encounters an error, and Wait one second between retries (I’ve set it this low as the backup runs late at night, as if someone has left a file open it doesn’t matter how many retries are attempted).

Following this, the /LOG switch dumps the full output of the copy job to n:\logs\data.log. This includes information on the number of files copied, directories excluded, etc….

Finally, we get to the /XD switch. Note that this is the last included switch – although you can technically have it where ever you want in the command, it’s cleaner, and less error-prone if you make it the last switch.

To exclude multiple directories, simply list them with spaces in between. If the path contains spaces (in the example above, I’ve excluded System Volume Information), wrap it in double-quotes (“). The most important thing, though, is DO NOT EVER, *EVER* include a trailing “\” in excluded paths!

For some reason, ROBOCOPY parses the trailing “\” in an odd way. If included in the command, the log will list all of the excluded paths, however it will still copy the data. It won’t provide any error messages or other output. For example, if you used the following command:

robocopy d: n:\Data /MIR /Z /A-:R /R:1 /W:1 /LOG:n:logsdata.log /XD d:\vms d:\wsus “d:\System Volume Information\” d:\tmpbak d:\installs d:\RECYCLER

Because there is a trailing \ on “d:\System Volume Information\”, the log will report that the excluded directories are d:\vms d:\wsus d:\System Volume Information d:\tmpbak d:\installs d:\RECYCLER, however it will still attempt to copy all of them.

However, if you simply omit the trailing \, ROBOCOPY will correctly parse the list of paths and will ignore them.