Sandeep
Sandeep

Reputation: 5771

How to exclude a file type in XCOPY without passing a file?

I have 5 txt files in my C drive, say

result_1.0.1.txt
result_1.0.1_tmp1.txt
result_1.0.1_tmp2.txt
result_1.0.1_tmp3.txt
result_1.0.1_tmp4.txt

At any point, there will be only 1 valid result txt file (in this case it is result_1.0.1.txt), and multiple tmp files. I want to copy only the result_1.0.1.txt using a generic XCOPY command. The version number in the file can be different each time. Earlier, only the result_<version>.txt file used to be present, so my XCOPY command looked like this

xcopy /Y "C:\*.txt" result.txt

To exclude the 'tmp' files, XCOPY requires a filename as the argument for /EXCLUDE parameter. Instead of that, is there a way to pass a wildcard argument? Something like this -

xcopy /Y "C:\*.txt" /EXCLUDE:"*tmp*" result.txt

Upvotes: 4

Views: 2714

Answers (1)

jdaz
jdaz

Reputation: 6043

Per the explanation here, you need to create a new file with the patterns you want to exclude. Using a wildcard is not possible.

So you can create a file exclusions.txt (in a different directory) with the contents:

_tmp

That's all.

Then you can run

xcopy /Y "C:\*.txt" /EXCLUDE:exclusions.txt result.txt

You can also generate exclusions.txt dynamically as is done in this Super User answer.

Or, if your file names will always be in the exact format you're showing, you could skip using EXCLUDE entirely and instead use something like:

xcopy /Y "C:\*.?.txt" result.txt

The ? is a single character wildcard, so this will match any file ending in .1.txt, .2.txt, etc., which your tmp files don't have. You could even get more specific and use *.?.?.txt or result*.?.?.txt.

Upvotes: 3

Related Questions