Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6565121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:03:15+00:00 2026-05-25T14:03:15+00:00

I wonder how to synchronize two folders including subfolders using MSBuild. What I like

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:03:16+00:00Added an answer on May 25, 2026 at 2:03 pm

    You can do it with the GetDistinctItems task from MSBuild Extension pack.
    The basic idea is to get the distinct items between files from the source and the destination folder.

    <Project ToolsVersion="3.5" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks" />
    
      <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" />
    
        <!-- Remove all files from the dest folder 
                that don't exist in the source folder -->
        <ItemGroup>
          <DestFiles Include="$(DestFolder)\**"/>
        </ItemGroup>
    
        <!-- Cannot compare FilesToCopy with DestFiles directly, 
             root folders are different-->
        <ItemGroup>
          <SrcFilesLeave Include="%(FilesToCopy.RecursiveDir)%(FilesToCopy.Filename)%(FilesToCopy.Extension)"/>
          <DestFilesLeave Include="%(DestFiles.RecursiveDir)%(DestFiles.Filename)%(DestFiles.Extension)"/>
        </ItemGroup>
    
        <MSBuild.ExtensionPack.Framework.MsBuildHelper TaskAction="GetDistinctItems" 
                                                       InputItems1="@(SrcFilesLeave)" 
                                                       InputItems2="@(DestFilesLeave)">
          <Output TaskParameter="OutputItems" ItemName="Distinct"/>
        </MSBuild.ExtensionPack.Framework.MsBuildHelper>
    
        <Message Text="Distinct %(Distinct.Identity)"/>
        <Delete Files="$(DestFolder)\%(Distinct.Identity)" />
      </Target>
    
    </Project>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wonder why is this happening all the time...!! I wrote two programs one
I wonder, if a solution exists, which statically analyzes PHP source-files. I'm aware of
I was wonder about the proper usage of ManualWorkflowSchedulerService.RunWorkflow(). Do I need to synchronize
I wonder if there is a way to synchronize objects/methods in JavaScript in a
I wonder if there is a way for me to SCP the file from
I wonder how to add UI elements programatically to existing nib files. If I
i wonder what is the best solution to send an email from my app.
I wonder if it is possible to supress errors in PHP like when redeclaring
I wonder, does there exist something like Velocity , but for internal use? Essentially
I wonder if MailMessage class is protected from e-mail injection . For example, should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.