Msbuild 使用唯一的文件名复制并展平

发布于 2024-08-18 16:26:25 字数 803 浏览 3 评论 0原文

我正在尝试从深层源代码树中复制具有相同文件名多个文件。例如 TestResults.trx。我想将它们复制到单个目录(即扁平化)中。问题是它们只是互相覆盖,我最终在目录中只得到一个 TestResults.trx 。

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

我想我可以使用一些 众所周知的元数据进行转换 但似乎没有任何独特的东西可以做到这一点(我试图在这样的目录中实时复制测试结果:.\SomeProject\bin\debug\TestResults.trx)。

像这样复制到这样的目录是理想的:

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

我不关心实际名称,只要它们是唯一的即可。

有什么想法,看起来需要自定义任务吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里人 2024-08-25 16:26:25

我无法提供仅使用 msbuild 的解决方案 - 您可以使用 msbuildtasks
使用任务来递增计数器。

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

但是,您可能会通过自定义任务或执行 powershell 脚本做得更好。

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.

樱娆 2024-08-25 16:26:25

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文