Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to get robocopy to exclude any file whose path contains a directory with a particular name. For example, "bar":

c:\foo\bar\a.txt
c:\bar\c.txt
d:\baz\bar\flub\d.txt

should be excluded, but not

c:\foo\barf\b.txt

Here are the things I tried -- each of which was rejected by robocopy:

/XD \bar\
/XD *\bar\*
/XD *^\bar^\*

Any ideas?

share|improve this question

Turns out the /XD matches on the directory name -- not the full path. So you don't actually have to worry about matching the backslashes.

So to exclude directories (anywhere in the path) called "bar", a simple /XD bar will work. If you wanted to exclude "barf", too, you could use /XD bar*.

Sometimes the answer is simpler than you think.

share|improve this answer

Part of the failure is using a trailing \ in Robocopy when referring to a directory - which is a no-no - as Robocopy tries to interpret it as an escape character. Almost everyone makes this mistake in one way or another with Robocopy. With that in mind, this example will work:

/XD "*\Bar"

If you wanted to match a more specific folder structure (so as not to match other subfolders, you can add more detail, like:

/XD "*\Foo\Bar"
/XD "C:\Foo\Bar"
share|improve this answer
    
Wildcard exclusions don't work. Robocopy will throw an error if you try. Did you test your examples? – user1751825 May 2 at 1:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.