使用MSBUILD同步两个文件夹

使用MSBUILD同步两个文件夹,msbuild,Msbuild,我想知道如何使用MSBuild同步两个文件夹,包括子文件夹 我喜欢做的是 a) 将源文件夹中的所有文件复制到较新的dest文件夹 或者在dest文件夹中不存在 及 b)从dest文件夹中删除数据库中不存在的所有文件 源文件夹 a) 使用任务很容易,但我如何才能完成b) 这是到目前为止我的生成文件: C:\来源 C:\dest 您可以使用来自的getDifferentitems任务完成此操作。 基本思想是从源文件夹和目标文件夹中获取文件之间的不同项 <Project ToolsVersio

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

我喜欢做的是

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

b)从dest文件夹中删除数据库中不存在的所有文件 源文件夹

a) 使用
任务很容易,但我如何才能完成b)

这是到目前为止我的生成文件:


C:\来源
C:\dest

您可以使用来自的
getDifferentitems
任务完成此操作。 基本思想是从源文件夹和目标文件夹中获取文件之间的不同项

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

C:\来源
C:\dest

您可以在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"/>

contentPath=C:\inetpub\Dir1\
contentPath=C:\inetpub\Dir2\,computerName='https://$(RemoteComputerName):8172/MSDeploy.axd',用户名='$(MSDeployUsername)',密码='$(MSDeployPassword)',身份类型='$(MSDeployAuth)'
-动词:$(msdeployerb)-源:$(\u MSDeploySrc)-目的:$(\u MSDeployDest)$(MSDeployAdditionalParams)

文件夹可以在同一台计算机上,也可以在不同的计算机上。文件夹的路径可以包含本地路径和网络路径。等等您可以同步IIS网站和文件夹。只需使用不同的MSDeploy提供程序:
有关配置Web部署处理程序的帮助:

以防有人不想要或无法安装扩展包

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

C:\来源
C:\dest

效果很好!我安装了MSBuild扩展,并将
添加到构建文件中。我知道这是一个非常古老的线程,但这只是为寻找此解决方案的人员提供的一些额外信息(我确信我在其他地方找到了此解决方案或非常类似的解决方案)。请注意,此方法将在目标文件夹中保留空目录,因为“删除”任务仅删除文件。