/exclude in xcopy just for a file type
Asked Answered
H

5

80

I have a batch file to copy over files from Visual Studio to my Web folder. I want to copy all files in my web project, EXCEPT for *.cs files. I can't seem to get this to work:

xcopy /r /d /i /s /y /exclude:".cs" C:\dev\apan C:\web\apan

Any tips? I just get exit code 4 when I try to run this.

Hereabout answered 23/11, 2010 at 2:23 Comment(0)
R
169

The /EXCLUDE: argument expects a file containing a list of excluded files.

So create a file called excludedfileslist.txt containing:

.cs\

Then a command like this:

xcopy /r /d /i /s /y /exclude:excludedfileslist.txt C:\dev\apan C:\web\apan

Alternatively you could use Robocopy, but would require installing / copying a robocopy.exe to the machines.

Update

An anonymous comment edit which simply stated "This Solution exclude also css file!"

This is true creating a excludedfileslist.txt file contain just:

.cs

(note no backslash on the end)

Will also exclude all of the following:

  • file1.cs
  • file2.css
  • dir1.cs\file3.txt
  • dir2\anyfile.cs.something.txt

Sometimes people don't read or understand the XCOPY command's help, here is an item I would like to highlight:

Using /exclude

  • List each string in a separate line in each file. If any of the listed strings match any part of the absolute path of the file to be copied, that file is then excluded from the copying process. For example, if you specify the string "\Obj", you exclude all files underneath the Obj directory. If you specify the string ".obj", you exclude all files with the .obj extension.

As the example states it excludes "all files with the .obj extension" but it doesn't state that it also excludes files or directories named file1.obj.tmp or dir.obj.output\example2.txt.

There is a way around .css files being excluded also, change the excludedfileslist.txt file to contain just:

.cs\

(note the backslash on the end).

Here is a complete test sequence for your reference:

C:\test1>ver

Microsoft Windows [Version 6.1.7601]

C:\test1>md src
C:\test1>md dst
C:\test1>md src\dir1
C:\test1>md src\dir2.cs
C:\test1>echo "file contents" > src\file1.cs
C:\test1>echo "file contents" > src\file2.css
C:\test1>echo "file contents" > src\dir1\file3.txt
C:\test1>echo "file contents" > src\dir1\file4.cs.txt
C:\test1>echo "file contents" > src\dir2.cs\file5.txt

C:\test1>xcopy /r /i /s /y .\src .\dst
.\src\file1.cs
.\src\file2.css
.\src\dir1\file3.txt
.\src\dir1\file4.cs.txt
.\src\dir2.cs\file5.txt
5 File(s) copied

C:\test1>echo .cs > excludedfileslist.txt
C:\test1>xcopy /r /i /s /y /exclude:excludedfileslist.txt .\src .\dst
.\src\dir1\file3.txt
1 File(s) copied

C:\test1>echo .cs\ > excludedfileslist.txt
C:\test1>xcopy /r /i /s /y /exclude:excludedfileslist.txt .\src .\dst
.\src\file2.css
.\src\dir1\file3.txt
.\src\dir1\file4.cs.txt
3 File(s) copied

This test was completed on a Windows 7 command line and retested on Windows 10 "10.0.14393".

Note that the last example does exclude .\src\dir2.cs\file5.txt which may or may not be unexpected for you.

Rabassa answered 23/11, 2010 at 2:30 Comment(11)
Oh, that's a bit of an inconvenience since I am trying to distribute this to my entire development team. Are there any work-arounds to this?Hereabout
Updated answer to include robocopy alternative.Rabassa
Ok, I got it! I can just create the file at the beginning of the batch file: echo .cs > C:\nocs.txtHereabout
I see we found that at the same time. Thank you, Dean!Hereabout
Updated to reference possible unexpected side effects of exclusion and add complete example.Rabassa
Note that to generate exactly what Dean Taylor had (especially the backslash), you'd do this: echo .cs\ > excludedfileslist.txt Writing directly to C:\ is also discouraged these days, so if a relative path isn't ideal (it is for me), you might try echo .cs\ > %USERPROFILE%\excludedfileslist.txt and reference the same laterBellman
Erm, I neglected quotes to handle spaces if there are any: echo .cs\ > "%USERPROFILE%\excludedfileslist.txt"Bellman
The echo xx.xx > excludes.txt trick is pure gold, thank you for the idea!Effluent
Tried this. Wanted to exclude all files containing ~ because I have a number of programs, including my editor, that make backup file names containing ~, and I never use the character in file names myself. Maybe that is a special case? It copied files with ~ in their names! :(Unstoppable
~ wasn't the special case. The problem I had was not putting CR at the end of the line, just before the LF.Unstoppable
@JonCoombs But don't put the quotation marks around the "/exclude:%USERPROFILE%\excludedfileslist.txt" -- xcopy can't handle them.Ultrastructure
T
9

In my case I had to start a list of exclude extensions from the second line because xcopy ignored the first line.

Thirtytwo answered 18/12, 2013 at 10:46 Comment(2)
I just had this and leaving the first line of the exclude text file blank was the answer - anyone know why the first line has to be blank?Blucher
My guess at the reason for this is your excludedfileslist.txt was created in a UTF-8 capable editor and includes a BOM header as the first two bytes of the file.Rabassa
F
5

Change *.cs to .cs in the excludefileslist.txt

Floristic answered 15/11, 2012 at 20:20 Comment(0)
M
2

For excluding multiple file types, you can use '+' to concatenate more lists (containing the types of files to be excluded, as the accepted answer shows). For example:

xcopy /r /d /i /s /y /exclude:excludedfileslist1.txt+excludedfileslist2.txt C:\dev\apan C:\web\apan

Source: http://www.tech-recipes.com/rx/2682/xcopy_command_using_the_exclude_flag/

Malamut answered 2/12, 2014 at 3:16 Comment(3)
Or simply add each extension to separate lines in a single exclude file.Eckhardt
Beware this explanation can be understood the wrong way. The /EXCLUDE option does NOT specify files to exclude. It specifies files containing lists of files to exclude. (#43836672)Anisometropia
Thanks for the suggestion @ihebiheb, I clarified the answer.Malamut
T
0

Not sure if this has been mentioned, but unusually, the path/filename after the /EXCLUDED: switch cannot contain spaces and cannot be in quotes

Toga answered 2/2, 2023 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.