Search code examples
tfsmsbuildmsbuild-4.0

msbuild copy entire folder with same folder name


I'm trying to copy entire folder located in different places in TFS.

I have the following :

<Target Name="BuildControles">

    <ItemGroup>
      <Controles Include="$(BUILD_SOURCESDIRECTORY)\ABC\FOLDERB\*.*" />
      <Controles Include="$(BUILD_SOURCESDIRECTORY)\joe\bloe\FOLDERA\*.*" />
    </ItemGroup>

    <Copy SourceFiles="@(Controles)" DestinationFiles="@(Controles->'$(OutDir)metadata\[FOLDERA OR FOLDER B]\%(Filename)%(Extension)')" />
</Target>

I need those folders specified in the ItemGroup (FOLDERA and FOLDERB) copied to a specific path located under "metadata" folder so I have in the end :

..metadata\FOLDERA..

..metadata\FOLDERB..

Tried many different things with %(Directory), %(RecursiveDir) or %(RelativeDir) but can't find a way to do it.

Please be gentle I'm just starting with MsBuild :)


Solution

  • In msbuild you can add metadata to items, which basically are properties attached to items which you can then refer to with %() syntax. This seems suitable for what you want here:

    <ItemGroup>
      <Controles Include="$(BUILD_SOURCESDIRECTORY)\ABC\FOLDERB\*.*" >
        <Dest>FOLDERB</Dest>
      </Controles>
      <Controles Include="$(BUILD_SOURCESDIRECTORY)\joe\bloe\FOLDERA\*.*">
        <Dest>FOLDERA</Dest>
      </Controles>
    </ItemGroup>
    
    <Copy SourceFiles="@(Controles)"
          DestinationFiles="@(Controles->'$(OutDir)metadata\%(Dest)\%(Filename)%(Extension)')" />