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?
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.