Monday, June 23, 2008

Copy Multiple Files via TFS build

bI have recently been doing a lot of work with TFS and want to write about a few things I had to figure out how to do. As part of my build process I had a number of dlls I needed to copy out to my staging environment. To do this I overrode the AfterCompile target. In order to override this Target you have to import the Microsoft.Common.targets file.

<Import Project="$(MSBuildBinPath)\Microsoft.Common.targets" />

In MSBuild you can call Copy which will move a file from one location to another location. The key word here is that it will move "a file", it will not move "files." So you can call something like this:
<Copy SourceFiles="$(DeployStagePath)\my.dll"
          DestinationFolder="$(DeployPath)" />
But you cannot use code like this to copy all dll files:
<Copy SourceFiles="$(DeployStagePath)\*.dll"
          DestinationFolder="$(DeployPath)" />

To copy all dlls in a folder you need to create a new item. Here is the code I used to do this.

<Target Name="AfterCompile" DependsOnTargets="CopyFiles"> 
    <Message Text="The after build event sets up the compiled code for ClickOnce Deployment" Importance="high"/>

    <!-- Set up the items for doing a copy of a collection of files -->
    <CreateItem Include="$(DeployStagePath)\*.dll">
      <Output TaskParameter="Include" ItemName="DLLsToCopy"/>

    <Copy SourceFiles="@(DLLsToCopy)" DestinationFolder="$(DeployPath)" />


lokesh said...


Thankyou for your Post. I tried the same way as you said. It copies all the files but at two locations. At the specified location and at the base folder. Actually I have a folder with Input Queries in the actual project path and I need to copy the folder with all SQL files at the destination. I specified Actual source folder path as "$(SolutionRoot)\Tools\BarTask\Input Queries\*.sql" and destination path as DestinationFolder="$(DropLocation)\$(BuildNumber)\Release\BarTask\Input Queries\". After build all the files are copying to ..\Input Queries\ folder as well as in the ..\Release\ foler. Any help would be appreciated.

electronic document management said...

TFS providing very nice feature of copying multiple files.This post is helpful for performing the given task.All the steps and coding given in the post make things easy for me to implement.