Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do an MSBuild copy command that doesn't overwrite existing files?

Tags:

msbuild

Looking at the documentation for the Copy task, I don't see an obvious way to copy files without overwriting existing files at the destination. I only want to copy new files.

What I have so far:

<ItemGroup> 
  <Packages Include=".nuget-publish\*.*"  />
</ItemGroup>

<Copy SourceFiles="@(Packages)" DestinationFolder="\\server\nuget\packages\" />
like image 530
Jim Bolla Avatar asked Feb 14 '14 19:02

Jim Bolla


2 Answers

Condition attribute can be used to filter source files:

<Copy SourceFiles="@(SourceFiles)" DestinationFiles="$(DestinationPath)%(RecursiveDir)%(Filename)%(Extension)" Condition="!Exists('$(DestinationPath)%(RecursiveDir)%(Filename)%(Extension)')" />
like image 152
Raman Zhylich Avatar answered Sep 20 '22 03:09

Raman Zhylich


In your link there is an attribute "SkipUnchangedFiles". Add that to the copy task and set it to "true".

<Copy SourceFiles="@(Packages)" DestinationFolder="\\server\nuget\packages\" SkipUnchangedFiles="true" />  

EDIT: I set up a sample project with the following.

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup> 
        <ExistingPackages Include="dest\*.*" />
        <Packages Include="src\*.*" Exclude="@(ExistingPackages -> 'src\%(FileName)%(Extension)')" />
    </ItemGroup>
    <Target Name="Build">
        <Message Text="PackagesToCopy @(Packages)" Importance="high" />
    </Target>
</Project>

Folder + file taxonomy is:

src\
    doc1.txt
    doc2.txt
    doc3.txt
    doc4.txt
    doc5.txt
    doc6.txt
dest\
    doc2.txt
    doc4.txt
    doc6.txt
CopyNew.proj

When I run msbuild.exe CopyNew.proj, I get the following output:

Build:
  PackagesToCopy src\doc1.txt;src\doc3.txt;src\doc5.txt

So now @(Packages) no longer contains the files that exist in the destination folder!

like image 22
Nicodemeus Avatar answered Sep 18 '22 03:09

Nicodemeus