-1

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?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Alexander
  • 69
  • 7
  • 2
    Type `xcopy /?` and look at the `/EXCLUDE` paramater. – Gerhard Feb 10 '22 at 10:33
  • 1
    1. Your quotation style is terrible, always quote the whole paths: `xcopy /S /Y /R "C:\SourceCodeOld\Release\Source Code\*.vb" "C:\SourceCodeNew\Source Code\"`. 2. Type `xcopy /?` into Command Prompt and read the output! – aschipfl Feb 10 '22 at 10:41
  • 1
    How about this for an idea? Don't use `xcopy.exe`, use `Robocopy.exe` instead. Robocopy was released as a successor to xcopy, way back in Windows NT, and as such there is rarely a need to use xcopy. If you open a Command Prompt window, type `robocopy /?`, and press the `[ENTER]` key, you'll see that it has options to eXclude Files and Directories matching given names/paths/wildcards. i.e. `/XF file [file]...`, `/XD dir [dir]...`. Using this allows you to exclude directly from the command, without having to pre-create a file with one or more names/paths/wildcards. – Compo Feb 10 '22 at 11:04

2 Answers2

2

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 even use wildcards in these strings. Furthermore, 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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • I'll vote for this because I was going to mention `robocopy` but I was too lazy `:p` – Gerhard Feb 10 '22 at 11:44
  • 1
    @Alexander `%SystemRoot%\System32\xcopy.exe` is deprecated since Windows Vista which means since more than 15 years. The successor is `%SystemRoot%\System32\robocopy.exe` which should be used for copying or moving multiple files with directory tree. __ROBOCOPY__ is more robust and has more options. – Mofi Feb 10 '22 at 17:49
0

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 /?

Gerhard
  • 22,678
  • 7
  • 27
  • 43