Home > Mobile >  XCOPY - How to exclude a certain file name?
XCOPY - How to exclude a certain file name?

Time:02-10

This is my current xcopy command:

xcopy C:\SourceCodeOld\Release\"Source Code"\*.vb C:\SourceCodeNew\"Source Code"\ /S /Y /R

In several subfolders, I have a file named "AssemblyInfo.vb". How can I exclude it from being copied?

CodePudding user response:

There is an /EXCLUDE option of xcopy, which allows to specify (the path to) a text file that contains partial file paths/names one per line (note also the fixed quotation):

xcopy /S /Y /R /I /EXCLUDE:exclude.txt "C:\SourceCodeOld\Release\Source Code\*.vb" "C:\SourceCodeNew\Source Code"

with exclude.txt being in the current working directory and containing:

AssemblyInfo.vb

However, the implementation of the /EXCLUDE option is terrible, because actually all files are excluded whose absolute source paths contain any of the given strings at any position (in a case-insensitive manner); moreover, you cannot provide a quoted path to the exclusion text file to protect potential spaces or special characters. (Refer also to this related answer of mine.)


I strongly recommend to use the robocopy command, whose exclusion options are more mature:

robocopy "C:\SourceCodeOld\Release\Source Code" "C:\SourceCodeNew\Source Code" "*.vb" /S /XF "AssemblyInfo.vb"

This truly excludes only files whose names are AssemblyInfo.vb.

CodePudding user response:

Create a file excludes.txt and add the files to exclude to it (each in a new line)

AssemblyInfo.vb
anotherfile.vb

Then run your xcopy command using the /EXCLUDE parameter pointing to the file that contains the files to exclude:

xcopy "C:\SourceCodeOld\Release\Source Code\*.vb" "C:\SourceCodeNew\Source Code\" /EXCLUDE:excludes.txt /S /Y /R

To see the various options available for the /EXCLUDE parameter, run xcopy /?

  • Related