简体   繁体   English

使 MSBuild 复制任务仅在源较新时进行复制,无论大小如何

[英]make an MSBuild Copy Task only copy if the source is newer regardless of size

I'm currently using an msbuild file to copy some files to the public documents folder when my EXE is compiled.我目前正在使用 msbuild 文件在编译我的 EXE 时将一些文件复制到公共文档文件夹。 My current script includes this:我当前的脚本包括:

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

This code only copies if the destination doesn't exist.此代码仅在目标不存在时复制。 However, I want to replace the destination if my source is newer.但是,如果我的源较新,我想替换目标。 How do I modify my script to make that happen?我如何修改我的脚本来实现这一点? I see the SkipUnchangedFiles flag, but it also compares file size to determine if the destination should be overwritten.我看到了 SkipUnchangedFiles 标志,但它还会比较文件大小以确定是否应该覆盖目标。 That is not what I want.那不是我想要的。

Your copy's conditional can be changed as follows:您的副本的条件可以更改如下:

    <Copy SourceFiles="%(DeploymentItems.FullPath)"
        DestinationFiles="$(PublicDocumentsFolder)%(Path)\%(RecursiveDir)%(Filename)%(DeploymentItems.Extension)"
            Condition="%(Filename)!='' AND (!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) = Modified Datetime of the source file %(ModifiedTime) = 源文件的修改日期时间

$([System.IO.File]::GetLastWriteTime($(PublicDocumentsFolder)%(Path)\\%(RecursiveDir)%(Filename)%(DeploymentItems.Extension))) = Modified Datetime of the destination file, if it exists $([System.IO.File]::GetLastWriteTime($(PublicDocumentsFolder)%(Path)\\%(RecursiveDir)%(Filename)%(DeploymentItems.Extension))) = 目标文件的修改日期时间(如果存在)

Let me know if this works or not, did not test.让我知道这是否有效,没有测试。

I think you may have misread the docs:我认为您可能误读了文档:

SkipUnchangedFiles跳过未更改的文件

If true, skips the copying of files that are unchanged between the source and destination.如果为 true,则跳过源和目标之间未更改的文件的复制。 The Copy task considers files to be unchanged if they have the same size and the same last modified time .如果文件具有相同的大小和相同的上次修改时间,则复制任务认为文件未更改

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM