Sometimes you need to copy files out to your build folder prior to running the program. I needed this for a deployment project where I wanted to copy the installer for SQL Server Express for my installation and then run the installer. I had a few errors as I set it up, but finally got everything working. The command line options I ended up using with my xcopy command are obvious once I learned to use them.

For this project, I am using Visual Studio 2008.

Go to your project’s properties and go to the Build Events tab. There are two boxes to fill in optionally. I chose the Post-Build Event Command Line, and added the following command. The$(xxx) are project parameters you can use in the build event. As you can see, I use the parameters for my solution’s directory, the project name and the output directory:

xcopy “$(SolutionDir)$(ProjectName)SQLServerSetupx64” “$(SolutionDir)$(ProjectName)$(OutDir)SQLServerSetupx64” /E /I /Y /D

Build Events

Build Events

These are the options I used:

/E – Copies directories and subdirectories, including empty ones.

/I – If destination does not exist and copying more than one file, assumes that destination must be a directory.

/Y – Suppresses prompting to confirm you want to overwrite an existing destination file.

/D – Copies files changed on or after the specified date. If no date is given (my case), copies only those files whose source time is newer than the destination time.
There were several possible errors that I saw while troubleshooting. If you use the above xcopy options, you should not see this one:

The command “xcopy “C:DevZenwaretrunkZenware.Database.InstallerZenware.Database.InstallerSQLServerSetupx64” “C:DevZenwaretrunkZenware.Database.InstallerZenware.Database.InstallerbinDebugSQLServerSetupx64″” exited with code 2.

This error came up because I had not provided the xcopy command line parameters listed above – any user interaction will cause this error, because when your installer runs there will be no user interaction. The command line parameters suppress any needed user interaction.

The other error I saw has to do with a valid folder path:

The command “xcopy “C:DevZenwaretrunkZenware.Database.InstallerZenware.Database.InstallerSQLServerSetupx64xxx” “C:DevZenwaretrunkZenware.Database.InstallerZenware.Database.InstallerbinDebugSQLServerSetupx64″” exited with code 4.

The nice thing about debugging this scenario, is the error message provides the command line with any build event parameters ($(xxx)) printed converted to their value in the error. You can copy the command line prompt for the xcopy and run it in a command window to see its behavior.

If there is anything that could clarify this article, please let me know.