Excluding Multiple Paths with ROBOCOPY

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.

Comments (4)

  1. 4:40 am, July 16, 2011pathe3  / Reply

    Good info

  2. 1:53 pm, December 30, 2011anonymous  / Reply

    Thanks a ton for this info!

  3. 12:56 pm, January 14, 2012Chris Duv  / Reply

    Very helpful, a pity that MS site does not hit basic worked examples and much sought after focus on attributes with the user will most likely be trying to achieve. In my case backing up data updates with out eliminating all data in destination drive folder, which I unwittingly performed on one of my first applications of /Mir.
    As pointed out in the MS tech site /Mir is really a combination of /E and /Purge. Where /E is copy all including replacing sub directories with up dates of all files and folders, and /purge is just an elimination of all original contents of destination location, replacing with source file &folder contents and structure.
    Using /E command both ways from destination to source has the effect of updating both with one anothers’ most recent versions or additions of files and folders so that drives will in effect be both updated.
    Typing file locations is an error prone exercise. I found best to use MS explorer highlight a sub directory of both source and destination folders, right click then select properties, then copy the folder location by highlighting inclusive of network and root drive location and right click selecting copy. These can then be pasted into txt file inside “” with robocopy commands, eliminating one source of error.
    Minor observation: I noticed that robocopying to “C:” with out specifying folders, will send result to location of robocopy executing bat file.
    Incidentally, in above text was LOG:n:logsdata.log above meant to read LOG:n:\logsdata.log with \ after n:? Shall check it myself later.
    Only been using this since Christmas, lot more for me to learn, please correct me or update my comments. In the meantime, posts like the above and forums like this are most helpful, thanks & GBU.

    • 12:59 pm, January 14, 2012Laslow  / Reply

      Ya, it should have slashes in that path – fixed! I had exported and reimported my blog posts to fix an issue, and it stripped the slashes from my posts.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

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