使用MSBUILD同步两个文件夹

时间:2011-09-15 08:10:56

标签: msbuild

我想知道如何使用MSBuild同步两个文件夹,包括子文件夹。

我喜欢做的是

a)将所有文件从源文件夹复制到较新的dest文件夹    或者不存在于dest文件夹中

b)删除目标文件夹中不存在(不再)的所有文件    源文件夹

a)使用<Copy>任务非常简单,但我怎样才能完成b)?

到目前为止,这是我的构建文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" 
    ToolsVersion="4.0" 
    DefaultTargets="Backup">
    <PropertyGroup>
        <SourceFolder>C:\source</SourceFolder>
        <DestFolder>C:\dest</DestFolder>
    </PropertyGroup>
    <ItemGroup>
        <FilesToCopy Include="$(SourceFolder)\**" />
    </ItemGroup>
    <Target Name="Backup">
        <!-- copy all files from the source folder to the dest folder 
            that are newer or don't exist in the dest folder -->
        <Copy 
            SourceFiles="@(FilesToCopy)" 
            DestinationFiles="@(FilesToCopy->'$(DestFolder)\%(RecursiveDir)%(Filename)%(Extension)')" 
            SkipUnchangedFiles="True" />
        <!-- TODO: remove all files from the dest folder 
            that don't exist in the source folder -->
    </Target>
</Project>

3 个答案:

答案 0 :(得分:4)

您可以使用MSBuild Extension pack中的GetDistinctItems任务执行此操作。 基本思想是从源文件夹和目标文件夹中获取文件之间的不同项。

<Project ToolsVersion="3.5" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks" />

  <PropertyGroup>
    <SourceFolder>C:\source</SourceFolder>
    <DestFolder>C:\dest</DestFolder>
  </PropertyGroup>

  <ItemGroup>
    <FilesToCopy Include="$(SourceFolder)\**" />
  </ItemGroup>

  <Target Name="Backup">
    <!-- copy all files from the source folder to the dest folder 
                  that are newer or don't exist in the dest folder -->
    <Copy SourceFiles="@(FilesToCopy)" 
          DestinationFiles="@(FilesToCopy->'$(DestFolder)\%(RecursiveDir)%(Filename)%(Extension)')" 
          SkipUnchangedFiles="True" />

    <!-- Remove all files from the dest folder 
            that don't exist in the source folder -->
    <ItemGroup>
      <DestFiles Include="$(DestFolder)\**"/>
    </ItemGroup>

    <!-- Cannot compare FilesToCopy with DestFiles directly, 
         root folders are different-->
    <ItemGroup>
      <SrcFilesLeave Include="%(FilesToCopy.RecursiveDir)%(FilesToCopy.Filename)%(FilesToCopy.Extension)"/>
      <DestFilesLeave Include="%(DestFiles.RecursiveDir)%(DestFiles.Filename)%(DestFiles.Extension)"/>
    </ItemGroup>

    <MSBuild.ExtensionPack.Framework.MsBuildHelper TaskAction="GetDistinctItems" 
                                                   InputItems1="@(SrcFilesLeave)" 
                                                   InputItems2="@(DestFilesLeave)">
      <Output TaskParameter="OutputItems" ItemName="Distinct"/>
    </MSBuild.ExtensionPack.Framework.MsBuildHelper>

    <Message Text="Distinct %(Distinct.Identity)"/>
    <Delete Files="$(DestFolder)\%(Distinct.Identity)" />
  </Target>

</Project>

答案 1 :(得分:2)

如果有人不想或不能安装扩展包

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" 
    ToolsVersion="4.0" 
    DefaultTargets="Backup">
    <PropertyGroup>
        <SourceFolder>C:\source</SourceFolder>
        <DestFolder>C:\dest</DestFolder>
    </PropertyGroup>
    <ItemGroup>
        <FilesToCopy Include="$(SourceFolder)\**" />
    </ItemGroup>
    <Target Name="Backup">
        <!-- copy all files from the source folder to the dest folder 
            that are newer or don't exist in the dest folder -->
        <Copy 
            SourceFiles="@(FilesToCopy)" 
            DestinationFiles="@(FilesToCopy->'$(DestFolder)\%(RecursiveDir)%Filename)%(Extension)')" 
            SkipUnchangedFiles="True">
            <Output TaskParameter="CopiedFiles" ItemName="Copied"/>
         </Copy>
         <ItemGroup>
            <OutdatedFiles Include="$(DestFolder)\**" Exclude="@(Copied)"/>
         </ItemGroup>
         <Delete Files="@(OutdatedFiles)"/>
    </Target>
</Project>

答案 2 :(得分:1)

您可以在MSBuild中使用MSDeploy实用程序来同步两个文件夹,如下所示:

<Target Name="SynchronyzeFolders">
  <PropertyGroup>
    <_MSDeploySrc>contentPath=C:\inetpub\Dir1\</_MSDeploySrc>
    <_MSDeployDest>contentPath=C:\inetpub\Dir2\,computerName='https://$(RemoteComputerName):8172/MSDeploy.axd',userName='$(MSDeployUsername)',password='$(MSDeployPassword)',authtype='$(MSDeployAuth)'</_MSDeployDest>
    <_MSDeployParameters>-verb:$(MSDeployVerb) -source:$(_MSDeploySrc) -dest:$(_MSDeployDest) $(MSDeployAdditionalParams)</_MSDeployParameters>
  </PropertyGroup>

  <Exec Command="msdeploy $(_MSDeployParameters)"
        CustomErrorRegularExpression="ERROR"/>

文件夹可以位于相同或不同的计算机上。文件夹的路径可以包含本地和网络路径。等等。您可以同步IIS网站和文件夹。只需使用不同的MSDeploy提供程序:http://technet.microsoft.com/en-us/library/dd569040(v=ws.10).aspx 有关配置Web部署处理程序的帮助:http://www.iis.net/learn/publish/using-web-deploy/configure-the-web-deployment-handler