MSBuild recursive copy

2019-08-10 10:57发布

问题:

In Csproj I have the following section:

<ItemGroup>
    <Content Include="C\MyPath\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Link>Res\%(RecursiveDir)%(Filename)%(Extension)</Link>
    </Content>

Thus if C\MyPath\ has a following structure:

-C
 -MyPath
  -f1.txt
  -folder1
   -f3.txt
   -folder4
  -folder2
   -f4.txt
   -folder5

I don`t want exactly the recursive copy but I want folder1 and folder2 not to be generated but rather start recursive copy from the second level:

-Res
 - f1.txt
 - f3,txt
 - folder4
 - f4.txt
 - folder5

How can I do it without copying from each folder separately:

 <Content Include="C\MyPath\folder1\**">
              <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
              <Link>Res\%(RecursiveDir)%(Filename)%(Extension)</Link>
    </Content>
<Content Include="C\MyPath\folder2\**">
              <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
              <Link>Res\%(RecursiveDir)%(Filename)%(Extension)</Link>
    </Content>

回答1:

Try MakeRelative property function:

<Copy SourceFiles="@(Content)"
          DestinationFolder="Res\$([MSBuild]::MakeRelative(%(Content.RelativeDir), %(Content.Filename)))" />