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 8013373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:37:02+00:00 2026-06-04T19:37:02+00:00

I have two directories ( dirA and dirB ), and i want to copy

  • 0

I have two directories (dirA and dirB), and i want to copy all files from dirA including sub-folders to dirB where a file in dirA replaces a file in dirB only if the two files have the same name (and also the same sub-path) and also fileB.LastWriteTime < fileA.LastWriteTime, or else if a file in dirA is not present in dirB.

So, if I have these directories:

<directory Name="dirA">

   <file Name="file1" LastWriteTime="10:00" />
   <file Name="file2" LastWriteTime="11:00" />
   <file Name="file7" LastWriteTime="12:00" />

   <directory Name="subDir">
      <file Name="file3" LastWriteTime="10:00" />
      <file Name="file4" LastWriteTime="11:00" />
   </directory>

   <directory Name="subDir2">
      <file Name="file5" LastWriteTime="10:00" />
      <file Name="file6" LastWriteTime="11:00" />
   </directory>

</directory>

<directory Name="dirB">

   <file Name="file1" LastWriteTime="09:00" />
   <file Name="file2" LastWriteTime="13:00" />

   <directory Name="subDir">
      <file Name="file3" LastWriteTime="09:00" />
      <file Name="file4" LastWriteTime="12:00" />
   </directory>

   <directory Name="BsubDir2">
      <file Name="file5" LastWriteTime="09:00" />
   </directory>

</directory>

After the update dirB will be:

<directory Name="dirB">

   <file Name="file1" LastWriteTime="10:00" />
   <file Name="file2" LastWriteTime="13:00" />
   <file Name="file7" LastWriteTime="12:00" />

   <directory Name="subDir">
      <file Name="file3" LastWriteTime="10:00" />
      <file Name="file4" LastWriteTime="12:00" />
   </directory>

   <directory Name="BsubDir2">
      <file Name="file5" LastWriteTime="09:00" />
   </directory>

   <directory Name="subDir2">
      <file Name="file5" LastWriteTime="10:00" />
      <file Name="file6" LastWriteTime="11:00" />
   </directory>

</directory>

Thanks.

  • 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-06-04T19:37:03+00:00Added an answer on June 4, 2026 at 7:37 pm

    Here a solution for you. Tried and tested with some files,
    Worked fine, but I think more test are needed

    Private Sub UpdateRec(Src As String, Dest As String, IsTest As Boolean)
    
        ' Update files in dest dir 
        For Each fileSrc As String In Directory.GetFiles(Src)
            Dim srcInfo As New FileInfo(fileSrc)
            Dim destFile As String = Path.Combine(Dest, srcInfo.Name)
            Dim foundFile As Boolean = File.Exists(destFile)
            if foundFile = true then
                Dim destInfo As New FileInfo(destFile)
              ' Copy only if newer 
              If Not IsTest AndAlso destInfo.LastWriteTime < srcInfo.LastWriteTime Then
                  File.Copy(srcInfo.FullName, destInfo.FullName, True)
              End If
            else
            'File not found in dest copy always
            If Not IsTest Then File.Copy(srcInfo.FullName, destFile)
            end if
        Next
    
        ' Update folder in dest
        For Each dirSrc As String In Directory.GetDirectories(Src)
            Dim srcInfo As New DirectoryInfo(dirSrc)
            Dim dstDir As String = Path.Combine(Dest, srcInfo.Name)
            Dim foundDir As Boolean = Directory.Exists(dstDir)
            ' Not found, create 
            if foundDir = false then
             Directory.CreateDirectory(dstDir)
            end if
            ' recurse to copy file and other subdirs
            Call Me.UpdateRec(dirSrc, dstDir, IsTest)
        Next
    End Sub
    

    Now it’s time to go to bed. Buonanotte.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two directories that contain files. I want to combine all these files
I have two or more directories which contains an ample of files. I want
Have two folders with approx. 150 java property files. In a shell script, how
I have two files client.php and server.php. The client file send a HTTP request
I have two directories with similar files, (one being a backup), what is a
I have two files with the same name Foo.rb in the directories pointed by
I want to compare the a list of files in two directories quickly. I
I have two directories with the same list of files. I need to compare
I have two directories with hundreds of PHP files in each. The second directory
I have files in two different directories that I would like to merge the

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.