Tuesday, January 6, 2009

The Xcopy /EXCLUDE switch

Have you ever struggled with the /EXCLUDE switch for the good ol´ xcopy DOS command? Well...I have...and the pitfalls described below shows that it can be a bit tricky.


Mission
Copy the content (files and subfolders) from a source directory to a given destination, but exclude some of the files in the source directory. The context in my case was a post-build event in a Visual Studio project that should perform a "developer deploy" to my local Sharepoint site.

Below is the folder structure in Visual Studio that I wanted to copy.

  • All .ascx files under CONTROLTEMPLATES should be copied, but the *.cs files should be excluded.
  • All files under IMAGES should be copied.
  • All files under LAYOUTS should be copied, except from the *.cs files.



Solution
The xcopy /EXCLUDE switch. Should be easy enough...

Pitfalls

  1. The /EXCLUDE:file1[+file2]... switch does not take a set of file-/foldernames that shall be excluded as parameter, but a set of textfiles that contains the file-/foldernames to exclude.
  2. The textfile(s) specifying the files to exclude uses CR+LF as delimitter for each filter string.
  3. The filter string can not include wildcards such as *.cs.
  4. The first line in the textfile(s) shall always be a CR+LF since that line will be skipped.
My post-build script that did the trick
REM === copy .dll to bin folder ===
copy "$(TargetPath)" "C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin\" /y


REM === copy CONTROLTEMPLATES ===
xcopy "$(ProjectDir)CONTROLTEMPLATES\*" "c:\program files\common files\microsoft shared\web server extensions\12\Template\CONTROLTEMPLATES" /E /I /Y /EXCLUDE:$(ProjectDir)XcopyExclude.txt


REM === copy IMAGES===
xcopy "$(ProjectDir)IMAGES\*" "c:\program files\common files\microsoft shared\web server extensions\12\Template\IMAGES" /E /I /Y /EXCLUDE:$(ProjectDir)XcopyExclude.txt


REM === copy LAYOUTS ===
xcopy "$(ProjectDir)LAYOUTS\*" "c:\program files\common files\microsoft shared\web server extensions\12\Template\LAYOUTS" /E /I /Y /EXCLUDE:$(ProjectDir)XcopyExclude.txt


The XcopyExclude.txt content
CR+LF <---- IMPORTANT!
.ascx.cs
.ascx.designer.cs
.aspx.cs
.aspx.designer.cs
.Master.cs
.Master.designer.cs


Note 1): The first line must (for some strange reason) include a CR+LF, since the first line always will be skipped.

Note 2) A single .cs filter would almost have done the trick, but in my specific case such a filter would have excluded the style.css file aswell.


And the soundtrack for this post?
http://www.deezer.com/track/120075

2 comments:

  1. hi, i am copying from x folder to y folder. X folder consits lot of files with different types like .xml, .entity, .gateway, .topic and i want to copy .xml files. Below is the command i am using for this but it doesnt work. can you please help me on this?

    xcopy "..\..\..\..\..\..\..\Build Output\Shared\Config\*" Config /I /Y /E /EXCLUDE:"C:\TCX Pilot\Main2\Build Output\Shared\Config\Exclude.txt"

    .txt file consits
    CR+LF
    .xml

    i am getting cant read that file......

    ReplyDelete
  2. You are awesome. I spent 3 hours to find why my exclusion list is not picked and found that it is all because the first line is always skipped from your post. It is so weird. However thanks a lot.
    ~Ranjith

    ReplyDelete