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

醉酒当歌 提交于 2020-04-08 04:57:55

问题


I'm currently using an msbuild file to copy some files to the public documents folder when my EXE is compiled. 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. That is not what I want.


回答1:


Your copy's conditional can be changed as follows:

    <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) = Modified Datetime of the source file

$([System.IO.File]::GetLastWriteTime($(PublicDocumentsFolder)%(Path)\%(RecursiveDir)%(Filename)%(DeploymentItems.Extension))) = Modified Datetime of the destination file, if it exists

Let me know if this works or not, did not test.




回答2:


I think you may have misread the docs:

SkipUnchangedFiles

If true, skips the copying of files that are unchanged between the source and destination. 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



来源:https://stackoverflow.com/questions/19665232/make-an-msbuild-copy-task-only-copy-if-the-source-is-newer-regardless-of-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!