MSbuild批处理 - 递归文件夹复制到多个目标文件夹

时间:2014-08-11 20:30:27

标签: recursion msbuild copy directory batching

我遇到了一个情况。我正在尝试使用MSBuild批处理将文件夹(子目录以及文件)复制到mutilple dest文件夹。 但是当我运行下面的脚本时,它会从根目标目录中转储src中的每个内容(子目录中的内容),而我所看到的是获得与目标目录中的src完全相同的结构。

<PropertyGroup>
        <Srcfldr>C:\helloworld\REService</Srcfldr>
    <DestFldr>C:\Projects\desire\Examples</DestFldr>
  </PropertyGroup>

  <ItemGroup>
    <SrcToCopy Include="$(Srcfldr)\*.*"/>
  </ItemGroup>

  <ItemGroup>
    <DestToCopy Include="$(DestFldr)/destfldr1"/>
    <DestToCopy Include="$(DestFldr)/destfldr2"/>
    <DestToCopy Include="$(DestFldr)/destfldr3"/>

  </ItemGroup>

   <Target Name="DeployBatching">
    <RemoveDir Directories="@(DestToCopy)"/>
    <MakeDir Directories="@(DestToCopy)"/>

    <Copy SourceFiles="@(SrcToCopy)" DestinationFolder="%(DestToCopy.FullPath)" />

你能告诉我我做错了什么......

SK

3 个答案:

答案 0 :(得分:0)

Vanilla复制任务更适合复制文件而不是目录,在任何情况下都要保留使用%(RecursiveDir)%(Filename)%(Extension)元数据重新映射目标所需的结构。请参阅second example on MSDN

编辑:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <Srcfldr>C:\helloworld\REService</Srcfldr>
        <DestFldr>C:\Projects\desire\Examples</DestFldr>
    </PropertyGroup>

    <ItemGroup>
        <SrcToCopy Include="$(Srcfldr)\**\*"/>
    </ItemGroup>

    <ItemGroup>
        <DestToCopy Include="$(DestFldr)\destfldr1"/>
        <DestToCopy Include="$(DestFldr)\destfldr2"/>
        <DestToCopy Include="$(DestFldr)\destfldr3"/>
    </ItemGroup>

    <Target Name="DeployBatching" Outputs="%(DestToCopy.FullPath)">
        <PropertyGroup>
            <DestToCopy>%(DestToCopy.FullPath)</DestToCopy>
        </PropertyGroup>
        <RemoveDir Directories="@(DestToCopy)"/>
        <MakeDir Directories="@(DestToCopy)"/>
        <Copy
            SourceFiles="@(SrcToCopy)"
            DestinationFiles="@(SrcToCopy->'$(DestToCopy)\%(RecursiveDir)\%(Filename)%(Extension)')"
        />
    </Target>
</Project>

答案 1 :(得分:0)

它看起来不像我想要的那样......我尝试了下面的代码

<PropertyGroup>
        <Srcfldr>C:\helloworld\REService</Srcfldr>
    <DestFldr>C:\Projects\desire\Examples</DestFldr>
  </PropertyGroup>

  <ItemGroup>
    <SrcToCopy Include="$(Srcfldr)\*.*"/>
  </ItemGroup>

  <ItemGroup>
    <DestToCopy Include="$(DestFldr)/destfldr1"/>
    <DestToCopy Include="$(DestFldr)/destfldr2"/>
    <DestToCopy Include="$(DestFldr)/destfldr3"/>

  </ItemGroup>

<PropertyGroup>
        <DestToCopyvar>%(DestToCopy)</DestToCopyvar>
      </PropertyGroup>

        <Target Name="DeployBatching">

          <Copy SourceFiles="@(SrcToCopy)" DestinationFiles="@(SrcToCopy->'$(DestToCopyvar)\%(RecursiveDir)%(Filename)%(Extension)')" />

它只复制根目录中的根文件,它一起缺少目录和子目录......

答案 2 :(得分:0)

这似乎对我有用......

<PropertyGroup>
<Srcfldr>C:\Msbuild\exproj\Rebinaries</Srcfldr>
<copyfldr>c$\component1</copyfldr>
</PropertyGroup>

<ItemGroup>
<SrcToCopy Include="$(Srcfldr)\**\*"/>
</ItemGroup>

 <ItemGroup>
 <DestToCopy Include="\\devsvr1\$(copyfldr);\\devsvr2\$(copyfldr)"/>
 </ItemGroup>

 <Target Name="DeployBatching" Outputs="%(DestToCopy.FullPath)">
    <PropertyGroup>
        <DestToCopy>%(DestToCopy.FullPath)</DestToCopy>
    </PropertyGroup>
    <RemoveDir Directories="@(DestToCopy)"/>
    <MakeDir Directories="@(DestToCopy)"/>
    <Copy
        SourceFiles="@(SrcToCopy)"
        DestinationFiles="@(SrcToCopy->'$(DestToCopy)\%(RecursiveDir)\%(Filename)%(Extension)')"
    />
</Target>