0
votes

I am running into a situation. I am trying to use MSBuild batching to copy a folder (subdirectories as well as files) to mutilple dest folders. but when i run the below script, it dumps every content from the src (contents from sub directories too) in root target directory, whereas what i was looking was to get the exact same structure as in src in the target dirs.

<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)" />

Can you please tell me what am i doing wrong ...

SK

3

3 Answers

0
votes

Vanilla copy task is better suited for copying files rather than directories, in any case to preserve structure you need to remap destination using %(RecursiveDir) and %(Filename)%(Extension) metadata. See second example on MSDN.

Edit:

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

Doesn't look like it's working the way i wanted it to ... I tried the below code

<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)')" />

It's copying just the root files in the root directory, it is missing the directories and sub directories al together ...

0
votes

This seems to be working for me now ...

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