Transfer Files After Project Build in Visual Studio
Cristian Merighi ()

How to use MSBuild to move files across projects while building.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.
A «quickie» about how to exploit Visual Studio (and in particular MSBuild) to transfer files after
some project's build.
To do that, it's needed to edit the very raw content of our project schema file. How? I think most of you know how, consider this a pleonastic refreshing:
- Right click on the project label in the solution explorer window (project containing the files to be moved).
- Select the item named Unload Project.
- Right click again on the project name (now labeled as unavailable).
- Select Edit [Nome_Progetto].[cs|vb]proj.
Now you're put in front of an XML document whose root is named Project (well, being more precise, its name's {http://schemas.microsoft.com/developer/msbuild/2003{Project}}).
Add to the root node the following markup block:
Markup
- <PropertyGroup>
- <ScriptsDestinationDirectory>E:\Components\Pacem 5\Pacem.TestWebsite\Scripts</ScriptsDestinationDirectory>
- <ScriptsSourceDirectory>Client</ScriptsSourceDirectory>
- </PropertyGroup>
- <Target Name="AfterBuild">
- <CreateItem Include="$(ScriptsSourceDirectory)\**\*.*">
- <Output TaskParameter="Include" ItemName="ScriptsFilesToCopy" />
- </CreateItem>
- <Copy SourceFiles="@(ScriptsFilesToCopy)" DestinationFiles="@(ScriptsFilesToCopy->'$(ScriptsDestinationDirectory)\%(RecursiveDir)%(Filename)%(Extension)')" />
- </Target>
What's notable in markup block:
- PropertyGroup node, we define here the involved variables, in this specific case ScriptsDestinationDirectory (physical path to target folder) and ScriptsSourceDirectory (relative path to source folder, named "Client").
- Target @Name="AfterBuild" node, we put here an Output Element whit relevant pointers to the specified variables.
- Transfer execution is described by the Copy node.
Hope that was helpful...
Update
Task can be equally accomplished - via project's properties panel - using Post-build event command-line.
Command-line syntax follows (notice that only .js files are recursively filtered in the example):
xcopy "$(ProjectDir)\Client\*.js" "$(SolutionDir)\Pacem.TestWebsite\Scripts\" /s /i /y
Take care. Bye.