A Mutable Log

A blog by Devendra Tewari


Project maintained by tewarid Hosted on GitHub Pages — Theme by mattgraham

xcopy

xcopy is a powerful command to repeatedly copy a directory and its sub-directories (including files) to a new destination. It can be particularly useful to developers in build scripts, or post-build event of a Visual Studio project. Here’s how I like to use it from the command line:

xcopy /Y /D /E /I /EXCLUDE:somefolder\exclude somefolder targetdir\somefolder

This will copy somefolder and all its files to targetdir/ The /Y option will suppress prompting the user for confirmation before replacing a file. The option /E tells xcopy to also copy empty folders. If you don’t want to copy empty folders, use the option /S instead. The /D option tells xcopy to only copy source files that have a more recent date and time than those at the target location. The /I option tells xcopy to create the destination folder if it does not exist.

The /EXCLUDE option will leave out files and directories listed (one per line) in the file called exclude. This can be useful if you are using some kind of version control system. For example, if you have a line with the text .copyarea.db, a file used by ClearCase Remote Client, those files in any folder will not be copied to the target location.

In the post-build event of a Visual Studio project xcopy can be used thus:

xcopy /D /E /I /EXCLUDE:$(ProjectDir)..\somefolder\exclude $(ProjectDir)..\somefolder $(TargetDir)somefolder

The $(ProjectDir) macro expands to the full (absolute) path where the project is located. The $(TargetDir) macro expands to the absolute path where the build output is written. I suggest avoiding absolute paths - use paths relative to the project location instead, as shown above.