can't exclude path with a space (xcopy)

2

I'm trying to use xcopy /exclude:exclude.txt and one of the paths in exclude.txt has a space in it and it's not working. Is there any workaround for this?

JoelFan

Posted 2011-06-02T16:52:49.410

Reputation: 3 012

Can you provide some examples of the paths in exclude.txt, including the path containing a space? – Velociraptors – 2011-06-02T17:51:13.343

Answers

4

Reading through the relevant sections of the xcopy help

/EXCLUDE:file1[+file2][+file3]...
         Specifies a list of files containing strings.  Each string
         should be in a separate line in the files.  When any of the
         strings match any part of the absolute path of the file to be
         copied, that file will be excluded from being copied.  For
         example, specifying a string like \obj\ or .obj will exclude
         all files underneath the directory obj or all files with the
         .obj extension respectively.

We can see that the exclude option is not working on paths or file names but "filters". To illustrate this I'll try to give a short example. Picture this exclude.txt

Unicorns
Dolphins

This will filter out any file that has unicorns or dolphins anywhere in its name. e.g Dolphins.txt will be filtered but Ponys.txt will be fine.

To get back to your issue. The reason that your filter isn't matching isn't because of the space in the path. By default xcopy will only care about the filename and not the full path, any filter you have that includes a full path will not match and the file will get copied.

You can change this behavior of xcopy by supplying the /f flag in your command. This should solve the issue you're seeing.

Paxxi

Posted 2011-06-02T16:52:49.410

Reputation: 6 952

Wow! /f works, but it this effect is totally undocumented! It is supposed to only affect printing messages, not how it interprets the Exclude file! – JoelFan – 2011-06-02T20:35:23.197

1

Edit exclude.txt and put double quotes around the path that contains a space.

Velociraptors

Posted 2011-06-02T16:52:49.410

Reputation: 1 207

0

One thing I've done in this situation is to use the "short name" (old style DOS 8.3 name). You can get this name with dir /x. Every Windows version since Win 95 that has allowed long file names has also allowed this alternate 8.3 name for files/dirs.

snapshoe

Posted 2011-06-02T16:52:49.410

Reputation: 939

using short file names can fail. By default windows creates short file names but it's possible to disable this using the fsutil tool. It's often suggested in tweaking guides to disable this so it's not something you want to design for. – Paxxi – 2011-06-02T18:09:43.770

Thanks Pär. I didn't realize that it could be disabled. – snapshoe – 2011-06-02T18:41:35.010