Windows – XCOPY can’t read /EXCLUDE file

batch fileenvironment-variableswindows 7xcopy

I'm trying to write a build script batch file for an Unreal Tournament 2004 mod I want to publish the source code for. This script contains a call to XCOPY with the /EXCLUDE parameter as follows:

XCOPY "%ProjectFolder%*" "%BuildFolder%" /S /C /Y /EXCLUDE:"%ProjectFolder%Build\CopyExcludes.cfg"

For some reason that command tells me it cannot read the CopyExcludes.cfg file. How can I fix that?

Within the batch file, %ProjectFolder% is the base folder of the source files, including a trailing backslash. %BuildFolder% is a subfolder within the UT2004 folder where the compiler will expect files to be. I ran the batch file with ECHO ON to see expanded variables:

C:\UT2004>XCOPY "C:\UT2004\EvenMatch\*" "C:\UT2004\EvenMatchV1" /S /C /Y /EXCLUD
E:"C:\UT2004\EvenMatch\Build\CopyExcludes.cfg"
Datei "C:\UT2004\EvenMatch\Build\CopyExcludes.cfg" kann nicht gelesen werden.

0 Datei(en) kopiert

(Nevermind the apparent line break in the command, that's just the CMD window hard-wrapping text after 80 characters.)

The weird part is, I can put the command type "%ProjectFolder%Build\CopyExcludes.cfg" right before and after the XCOPY command and it correctly shows the file content at both positions in the batch file:

C:\UT2004>type "C:\UT2004\EvenMatch\Build\CopyExcludes.cfg"
.svn
.bak
.git
.cfg
#
~

C:\UT2004>XCOPY "C:\UT2004\EvenMatch\*" "C:\UT2004\EvenMatchV1" /S /C /Y /EXCLUD
E:"C:\UT2004\EvenMatch\Build\CopyExcludes.cfg"
Datei "C:\UT2004\EvenMatch\Build\CopyExcludes.cfg" kann nicht gelesen werden.

0 Datei(en) kopiert

C:\UT2004>type "C:\UT2004\EvenMatch\Build\CopyExcludes.cfg"
.svn
.bak
.git
.cfg
#
~

The target folder for XCOPY exists and is empty. Both folders do not have any special access restrictions, at least none that I'm aware of. Process Explorer's find handle feature does not bring up anything when looking for "CopyExclude", i.e. the file doesn't seem to be locked by any other programm. I also already tried copying other subfolders individually, but XCOPY still fails to read the file. According to Notepad++, all lines in the file are terminated with CRLF.

I am using Windows 7 Pro x64, but hopefully can get this to work on any Windows version or edition.

Best Answer

The DOS command prompt, and individual commands, have different handling of quotes. Remove the double-quotes around the name of the exclude file and you should be set!

For filenames with spaces, you can use the 8-character version, e.g., PROGRA~1 for Program Files.

Related Question