I wonder how to synchronize two folders including subfolders using MSBuild.
What I like to do is
a) to copy all files from the source folder to the dest folder that are newer
or don’t exist in the dest folder
and
b) to remove all files from the dest folder that don’t exist (anymore) in the
source folder
a) is pretty easy using the <Copy> task but how can I accomplish b) ?
This is my build file so far:
<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" />
<!-- TODO: remove all files from the dest folder
that don't exist in the source folder -->
</Target>
</Project>
You can do it with the
GetDistinctItemstask from MSBuild Extension pack.The basic idea is to get the distinct items between files from the source and the destination folder.