开发者

How do I copy multiple files to the same directory without overwriting in MSBuild

开发者 https://www.devze.com 2023-02-15 23:12 出处:网络
I have multiple CodeCoverage.xml files in seperate directories and I want to copy them to a single directory with new names so that they don\'t get overwritten.

I have multiple CodeCoverage.xml files in seperate directories and I want to copy them to a single directory with new names so that they don't get overwritten.

<ItemGroup>
    <CoverageResultsXmlFiles Include="$(TestResultsDir)\**\CodeCoverage.xml" />
</ItemGroup>  

Do开发者_Python百科es anyone have an idea of how to copy these files to the $(TestResultsDir) without them being overwritten?

Is there a way to generate a random number that could be pre-pended to the filename so they would be unique?


You can do it with MSBuild 4.0 features:

  <Target Name="CopyCodeCoverage">
       <PropertyGroup>
           <DirIdentity>$(RecursiveDir.Replace(" ", "_"))</DirIdentity> 
           <DirIdentity>$(DirIdentity.Replace("\", "_"))</DirIdentity> 
           <UniqueIdentity>$([System.IO.Path]::GetRandomFileName())</UniqueIdentity>
           <DestCodeCoverage>$(DestinationDir)\$(DirIdentity)_$(UniqueIdentity)_CodeCoverage.xml</DestCodeCoverage>
       </PropertyGroup>
       <Copy SourceFiles="$(CodeCoverageFullPath)"
             DestinationFiles="$(DestCodeCoverage)"
             SkipUnchangedFiles="true" />
  </Target>

  <Target Name="ProcessResults">
          <MSBuild Projects="$(MSBuildProjectFullPath)"
                   Targets="CopyCodeCoverage"
                   Properties="CodeCoverageFullPath=%(CoverageResultsXmlFiles.FullPath);RecursiveDir=%(CoverageResultsXmlFiles.RecursiveDir);DestinationDir=$(TestResultsDir)" />
  </Target>

@Sayed Ibrahim Hashimi has excellent post about property functions and using static .Net methods. So you can implement any naming algorithm you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号