Msbuild copy and flatten with unique file names

I'm trying to copy multiple files from a deep source tree that have the same file name. For example TestResults.trx. I want to copy them into a single directory (i.e. flattened). Problem is they just overwrite each other and I just end up with a single TestResults.trx in the directory.

<ItemGroup>
  <SilverlightTestResults Include=".\**\*.trx" Exclude=".\TestResults\*" />
</ItemGroup>
<Copy SourceFiles="@(SilverlightTestResults)" DestinationFolder=".\TestResults">

I thought I could do a transform using some well known metadata but there doesn't seem to be anything unique in there to do it (the test results I'm trying to copy live in directories like this: .\SomeProject\bin\debug\TestResults.trx).

Copying to a directory like this like this would be ideal:

.\TestResults\TestResults1.trx
.\TestResults\TestResults2.trx
.\TestResults\TestResults3.trx

I don't care about the actual names as long as they are unique.

Any ideas, looks like a custom task is required?

Answers


I can't provide a solution that just uses msbuild - you could either use msbuildtasks to use the <Add /> task for incrementing a counter.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
    <PropertyGroup>
    <FileCounter>0</FileCounter>
</PropertyGroup>
    <ItemGroup>
        <MySourceFiles SilverlightTestResults Include=".\**\*.trx" Exclude=".\TestResults\*"/>
    </ItemGroup>
<Target Name="CopyFiles">
    <Math.Add Numbers="$(FileCounter);1">
        <Output TaskParameter="FileCounter" PropertyName="FileCounter" />
    </Math.Add>
    <Copy
        SourceFiles="@(MySourceFiles)"
        DestinationFiles="@(MySourceFiles->'.\TestResults\%(Filename)_$(FileCounter)%(Extension)')"
    />
</Target>

However you might do better with a custom task or probably executing a powershell script.


Yes, a custom task will be required.

You could look to see what functionality the Move task from the community task project (link here) offers, but if it doesn't do what you want then it is open source, so it will be trivial to check the source and modify it to suit your needs.


Need Your Help

How can I unit test a PHP class method that executes a command-line program?

php git unit-testing phpunit

For a PHP application I'm developing, I need to read the current git revision SHA which of course I can get easily by using shell_exec or backticks to execute the git command line client.

AccessViolation Exception in C# Web API

c# visual-studio web-api access-violation saprfc

Facing an error while accessing SAP RFC through C# Web API. Here are the details:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.