从msbuild中的多个目录复制文件

时间:2015-01-29 15:39:48

标签: msbuild

我有以下目录(例如)

./dirA/file1
./dirA/dir/file2
./dirB/file3
./dirB/dir/file4
./dirC/file5

我希望将它们复制到不同的地方,例如:

./dirA_renamed/file1
./dirA_renamed/dir/file2
./dirB_renamed_differently/file3
./dirB_renamed_differently/dir/file4
./dirC_renam/file5

目录列表及其新名称不会经常发生变化,但我只想使用一个Copy

我试过以下:

<ItemGroup>
 <ToCopy Include=".\dirA">
  <OutputDirName>dirA_renamed</OutputDirName>
 </ToCopy>
 <ToCopy Include=".\dirB">
  <OutputDirName>dirB_renamed_differently</OutputDirName>
 </ToCopy>
 <ToCopy Include=".\dirC">
  <OutputDirName>dirC_renam</OutputDirName>
 </ToCopy>
</ItemGroup>

...

<CreateItem Include="%(ToCopy.Directory)\**\*.*">
 <Output TaskParameter="Include" ItemName="FilesToCopy" />
</CreateItem>

<Copy SourceFiles="@(FilesToCopy)" DestinationFolder="@(FilesToCopy->'%(OutputDirName)')" />

但没有任何反应。如果我输出FilesToCopy,则为空。我做错了什么?

1 个答案:

答案 0 :(得分:1)

硬编码的方式是我所知道的,但我知道你可以使用参数,我还没有完全搞清楚,给你一个工作样本。

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
            ToolsVersion="4.0" DefaultTargets="Build">

  <PropertyGroup>
    <RootDir>C:\DevDir</RootDir>
    <SourceA>dirA</SourceA>
    <SourceB>dirB</SourceB>
    <SourceC>dirC</SourceC>
    <RenameA>dirA_renamed</RenameA>
    <RenameB>dirB_renamed_differently</RenameB>
    <RenameC>dirC_renam</RenameC>
  </PropertyGroup>

  <Target Name="CopyDirTest" >

    <ItemGroup>
      <SourceDirA Include="$(RootDir)$(SourceA)\"/>
      <SourceDirB Include="$(RootDir)$(SourceB)\" />
      <SourceDirC Include="$(RootDir)$(SourceC)\" />
    </ItemGroup>

    <ItemGroup>
      <SourceAFiles Include="$(RootDir)$(SourceA)\**\*.*"  />
      <SourceBFiles Include="$(RootDir)$(SourceB)\**\*.*" />
      <SourceCFiles Include="$(RootDir)$(SourceC)\**\*.*" />
      <SourceAllFiles Include="@(SourceAFiles);@(SourceBFiles);@(SourceCFiles)" />
    </ItemGroup>

    <CreateItem Include="@(SourceAFiles->Replace($(SourceA), $(RenameA)))">
      <Output TaskParameter="Include" ItemName="RenamedSourceA" />
    </CreateItem>

    <CreateItem Include="@(SourceBFiles->Replace($(SourceB), $(RenameB)))">
      <Output TaskParameter="Include" ItemName="RenamedSourceB" />
    </CreateItem>    

    <CreateItem Include="@(SourceCFiles->Replace($(SourceC), $(RenameC)))">
      <Output TaskParameter="Include" ItemName="RenamedSourceC" />
    </CreateItem>

    <ItemGroup>
      <RenamedAllFiles Include="@(RenamedSourceA);@(RenamedSourceB);@(RenamedSourceC)" />
    </ItemGroup>

    <Message Text="%(SourceAllFiles.Identity)" Importance="high" />
    <Message Text="%(RenamedAllFiles.Identity)" Importance="high" />

    <Copy SourceFiles="@(SourceAllFiles)" DestinationFiles="@(RenamedAllFiles)" />
  </Target>

</Project>