Search code examples
msbuildvisual-studio-2019msbuild-task

MSBuild task to Copy contents of a specific folder by using a wild card in the path


My Folder structure is as follows

Module
-->Asia - 1.0.0.12
------>Deployment
------>Install.exe
------>version.xml
-->Africa - 1.0.3.4
------>Deployment
------>Install.exe
------>version.xml
-->Europe - 2.0.1.2
------>Deployment
------>Install.exe
------>version.xml

I want to copy the 'deployment' folder (and subfolders) under each region to my output directory. The region numbers will change so i cannot hardcode them in my Include statement. The command i am using to copy for Asia region is

    <ItemGroup>
        <GetFiles Include="$(MSBuildProjectDirectory)\Module\Asia*\Deployment\**\*">
            <Destination>D:\Region\Asia</Destination>
        </GetFiles >
    </ItemGroup>
    <Copy
        SourceFiles="%(GetFiles.Identity)"
        DestinationFolder="%(GetFiles.Destination)\%(RecursiveDir)"
    />

Instead of the Deployment folder and its subdirectories getting copied under the Destination, i am getting the folder structure as

D:\Region\Asia\Asia - 1.0.0.12\Deployment

what i want is

D:\Region\Asia\Deployment

Can this be achieved? Thanks


Solution

  • MSBuild task to Copy contents of a specific folder by using a wild card in the path

    If you use wildcard in msbuild and do a copy task, the MSBuild will always copy the path from the first address where the wildcard is used.

    For your situation, since you just use wildcard under Asia*, so it will keep Asia - 1.0.0.12\Deployment folder structure under D:\Region\Asia.

    As a suggestion, to get what you want, you need to clearly indicate the name of the Asia folder, even if it is much more complicated, you need to specify one by one.

    Use like this:

        <Target Name="xxx" BeforeTargets="Build">
          <ItemGroup>
                <GetFiles Include="$(MSBuildProjectDirectory)\Module\Asia - 1.0.0.12\Deployment\**\*">
                    <Destination>D:\Region\Asia</Destination>
                </GetFiles>
         </ItemGroup>
         <Copy SourceFiles="%(GetFiles.Identity)"
               DestinationFolder="%(GetFiles.Destination)\Deployment\%(RecursiveDir)"/>
        
       </Target>