Why does xcopy’s /f option affect spaces in exclude files?

Posted on


I used asked this question and got an answer that seemed like it could not possibly be right, but I tried it and it worked!

How could the /f option, which according to the documentation :

Displays full source and destination file names while copying

possibly cause spaces to be recognized in the EXCLUDE file ?! This effect seems totally unrelated to the purpose of /f !


It sounds like the /f doesn’t affect just the display but also the filename data that is being used to match against exclude strings. Perhaps you are being overly rigorous in your interpretation of “display” :-).

But, seriously, keep in mind that documentation writers may not always foresee the ways different options might interact in the use of their tool. In other words, this behavior may not be by design but it works nonetheless.

I was the one giving you the earlier answer and you’re thinking about this the wrong way. The /f flag doesn’t affect how a space is handled but how paths are handled.

If you run xcopy without the /f flag you can see that it works on the files relative to the working directory or the paths you’ve given. Apparently this is also used by the internal logic of xcopy so when you run it by default it can only filter on the parts of the path you’ve specified in the copy command.

When you add the /f flag xcopy will display the full path of the file irregardless of how you invoked the copy. Since this seem to affect the internal logic this also makes it possible to filter on the full path of the file and not just the relative paths.

This is not explained in the current documentation of xcopy which leads you to believe that it only affects the display and not the internal logic.

You can test this for yourself with the following.

Create a folder c:unicorns and a folder c:ponys
In the unicorns folder, create three files: test1.txt, test2.txt and exclude.txt
Edit exclude.txt and paste the following


Go to c:unicorns and run the command

xcopy *.txt ..ponys /exclude:exclude.txt

You’ll see that it copies test2.txt and exclude.txt
Now add the /f flag as follows

xcopy /f *.txt ..ponys /exclude:exclude.txt

You’ll notice that it only copies exclude.txt this time.

In this example our working directory was c:unicorns and thus the names displayed only contained the filenames.

We can continue with this and show what happens if you use absolute paths. Use the same setup of directories as before and run the command

xcopy c:unicorns*.txt c:ponys /exclude:c:unicornsexclude.txt

Now you can see that the only file copied is exclude.txt. By giving the absolute paths i the command, they’re used in the internal logic and can be filtered upon.

If you try this with the /f flag you will notice that they work the same

xcopy /f c:unicorns*.txt c:ponys /exclude:c:unicornsexclude.txt

I hope this gives a clear explanation of how it currently works.

Leave a Reply

Your email address will not be published.