如果源是较新的,无论大小如何,都只复制MSBuild复制任务

时间:2013-10-29 17:30:09

标签: c# msbuild msbuild-task file-copying

我正在使用msbuild文件在编译EXE时将一些文件复制到公共文档文件夹。我目前的脚本包括:

<Target Name="DeployToPublicDocuments"
              Inputs="@(DeploymentItems)"
              Outputs="$(PublicDocumentsFolder)%(Path)\%(DeploymentItems.RecursiveDir)%(DeploymentItems.Filename)%(DeploymentItems.Extension)">
        <Copy SourceFiles="%(DeploymentItems.FullPath)"
            DestinationFiles="$(PublicDocumentsFolder)%(Path)\%(DeploymentItems.RecursiveDir)%(DeploymentItems.Filename)%(DeploymentItems.Extension)"
                Condition="!Exists('$(PublicDocumentsFolder)%(Path)\%(DeploymentItems.RecursiveDir)%(DeploymentItems.Filename)%(DeploymentItems.Extension)')" />

此代码仅在目标不存在时复制。但是,如果我的源更新,我想替换目标。如何修改我的脚本以实现这一目标?我看到SkipUnchangedFiles标志,但它也比较文件大小以确定是否应覆盖目标。那不是我想要的。

2 个答案:

答案 0 :(得分:13)

您的副本的条件可以更改如下:

    <Copy SourceFiles="%(DeploymentItems.FullPath)"
        DestinationFiles="$(PublicDocumentsFolder)%(Path)\%(RecursiveDir)%(Filename)%(DeploymentItems.Extension)"
            Condition="!Exists('$(PublicDocumentsFolder)%(Path)\%(RecursiveDir)%(Filename)%(DeploymentItems.Extension)') OR $([System.DateTime]::Parse('%(ModifiedTime)').Ticks) &gt; $([System.IO.File]::GetLastWriteTime('$(PublicDocumentsFolder)%(Path)\%(RecursiveDir)%(Filename)%(DeploymentItems.Extension)').Ticks)" />

%(ModifiedTime) =源文件的修改日期时间

$([System.IO.File]::GetLastWriteTime($(PublicDocumentsFolder)%(Path)\%(RecursiveDir)%(Filename)%(DeploymentItems.Extension))) =目标文件的修改日期时间(如果存在)

让我知道这是否有效,没有测试。

答案 1 :(得分:7)

我想你可能误解了文档:

  

SkipUnchangedFiles

     

如果为true,则跳过复制源和目标之间未更改的文件。复制任务认为如果文件具有相同的大小和相同的上次修改时间,则文件将保持不变。

http://msdn.microsoft.com/en-us/library/vstudio/3e54c37h.aspx