Thursday 6 February 2020

windows - can't exclude path with a space (xcopy)


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?



Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...