17

I have a directory structure like this:

folder1\
    help.txt
    folder2\
        help.txt
    folder3\
        help.txt
    ...

I want to copy the contents of folder1 using robocopy. For example:

robocopy folder1 destination /E

I want to exclude the help.text file that is in folder1 but include the help.txt files in folder2, folder3 etc. There may also be files named help.txt elsewhere in the directory structure so I don't want to have to hard code the paths to the files to include.

I can use the following command to exclude all files named help.txt but is there a way to just exclude the help.txt file in the root of folder1?

robocopy folder1 destination /E /XF help.txt

3 Answers 3

19

You must include the full path (including the drive) to the file to exclude. Relative paths won't work.

This works:

robocopy folder1 destination /E /XF "c:\somedir\another dir\folder1\help.txt"

This doesn't:

robocopy folder1 destination /E /XF "folder1\help.txt"

Nor does this:

robocopy folder1 destination /E /XF ".\folder1\help.txt"
3
  • You can exclude multiple files like so robocopy folder1 destination /E /XF "c:\file1" "c:\file2" Dec 18, 2014 at 18:24
  • @Dennis-Williamson, Thanks for this right answer. A pity that is doesn't work with wildcards: robocopy folder1 destination /E /XF "c:\somedir\another dir\folder1\help.txt*" leads to an error. Jul 25, 2015 at 8:28
  • So after 2 hours of trying other commands, this worked. Thanks a million! Nov 1, 2016 at 18:05
0

I don't see that there is any way to do that by filename. You could set an attribute on the file you don't want copied that you "know" isn't on any other files in the source tree and then use /xa to exclude files with that attribute.

Failing that, though, you're going to need to use another tool.

This will work:

echo folder1\help.txt > \temp\excl.txt
xcopy /E folder1 destination /EXCLUDE:\temp\excl.txt

Actually, if you had a directory structure that looked like this:

folder1\
  help.txt
  folder1\
    help.txt

it would probably exclude both of those files.

1
  • I see I'm wrong.
    – wfaulk
    Sep 24, 2009 at 17:03
0

You can use %CD% to give you the current directory. This should help you exclude specific directories. More help is here:

https://stackoverflow.com/a/5274061

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .