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.

11 Comments

  1. [...] This post was mentioned on Twitter by laslow. laslow said: New Blog Post – Excluding Multiple Paths with ROBOCOPY – http://www.laslow.net/?p=624 [...]

  2. pathe3 says:

    Good info

  3. anonymous says:

    Thanks a ton for this info!

  4. Chris Duv says:

    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.

    • Laslow says:

      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.

  5. guttolf says:

    Nice article, both to the point and guiding in a easy to read manner. Thank you for this. :)

    I have a follow up question though. I have set up robocopy to mirror a set of files and folders and exluding some lock files not wanted from the source. This works great. But would it be possible to delete any of these (now excluded) lock files in the destination? As it is now it ignores them both in source and destination. Syntax used:

    robocopy C:\path \\server.domain\share /MIR /V /LOG:log\example.log /XF *.lock *.lck

    • Laslow says:

      You could use del /s *.lock to remove files with that extension from the destination (the /s, as in most cases, tells it to scan the current directory and all subdirectories). The trick with this is because your destination is on a network drive you’d have to map it to a drive letter before you could actually browse to it. Alternatively, if you can browse to the share via Explorer, you could just hit F3, search for *.lock or *.lck and and remove them by hand.

      • mohamad says:

        let say if i have 200thousand folder with various name.

        i would like to copy folders which are start with a***** only.

        how can i do this?

        • Laslow says:

          Unfortunately, although ROBOCOPY has the ability to exclude folders by using wildcards (eg, /XD D:\a*), it doesn’t have the ability to implicitly include folders in the path.

          You’re best bet would likely be to use script (PowerShell or the like) to select the folders based on your criteria – this would be a good starting place: http://stackoverflow.com/questions/5844149/moving-files-by-starting-letter-in-powershell-python-or-other-scripting-languag

          • PhS says:

            Does ROBOCOPY have the ability to exclude folders by using wildcards in without reference to the “level” of the directory ? e.g. something like /XD #* would exclude directory D:\#abc and D:\123\#abc ?

            Thank you in advance

          • Laslow says:

            From robocopy’s /?:

            /XD dirs [dirs]… :: eXclude Directories matching given names/paths.

            For directories, it doesn’t look like it – you could specific a full name, but not a wildcard. Files, on the other hand:

            /XF file [file]… :: eXclude Files matching given names/paths/wildcards.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>