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.
Open the text and write the files' name you'd like to exclude in a separate line like this (without bullets) to make xcopy exclude multiple folders. 3. Run the command now: xcopy c:\t1 c:\t2 /EXCLUDE: C:\mybatch\MyExclusion. txt and press Enter.
The most important switches in this command are the /XD which allows you to exclude folders, and /XF that you can use to exclude files. The other options are optional, but you should use these options that you should use in any standard copy process using Robocopy.
xcopy cannot be configured to SKIP existing files, so you should copy using "robocopy".
If you want to copy only new files or changed files, you can use xcopy command in batch script file on Windows system. /i /d /y parameters provide that copy only new files and changed files. These detect file modify time changes , but do not notice size changes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With