Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

SynchronizeDirectoryTrees - Synchronize files in two directory trees

Total Hit ( 2104)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 



Click here to copy the following block
' Synchronize two directory subtrees
'
' This routine compares source and dest directory trees and copies files
' from source that are newer than (or are missing in) the destination directory

' if TWOWAYSYNC is True, files are synchronized in both ways

' NOTE: requires the CompareDirectories and SynchronizeDirectories routines
'    and a reference to the Microsoft Scripting Runtime type library

Sub SynchronizeDirectoryTrees(ByVal sourceDir As String, _
  ByVal destDir As String, Optional ByVal TwoWaySync As Boolean)
  Dim fso As New Scripting.FileSystemObject
  Dim sourceFld As Scripting.Folder
  Dim destFld As Scripting.Folder
  Dim fld As Scripting.Folder
  Dim col As New Collection
  
  ' we need this in case the dest subdir doesn't exist
  On Error Resume Next
  
  ' get reference to source and dest folder objects
  Set sourceFld = fso.GetFolder(sourceDir)
  Set destFld = fso.GetFolder(destDir)

  ' create the destination directory, if necessary
  If Err Then
    ' if the destination directory doesn't exist,
    ' create it and copy all files there
    ' (this is all we need)
    fso.CopyFolder sourceDir, destDir
    ' nothing else to do
    Exit Sub
  End If
  
  ' synchronize the root directories
  SynchronizeDirectories sourceDir, destDir, TwoWaySync
  
  ' ensure that dir names have a training backslash
  If Right$(sourceDir, 1) <> "\" Then sourceDir = sourceDir & "\"
  If Right$(destDir, 1) <> "\" Then destDir = destDir & "\"
  
  ' repeat for all the subdirectories in the source directory
  For Each fld In sourceFld.SubFolders
    ' remember that we have processed this subdir
    col.Add fld.Name, fld.Name
    
    ' call this routine recursively
    SynchronizeDirectoryTrees fld.Path, destDir & fld.Name, TwoWaySync
  Next
  
  ' if two-way synchronization was requested, ensure that all subdirs in dest
  ' directories are copied into source directory
  If TwoWaySync Then
    For Each fld In destFld.SubFolders
      If col(fld.Name) = "" Then
        ' we get here only if the folder name isn't in COL,
        ' and therefore
        ' if this subdirectory isn't in the source directory
        fso.CopyFolder fld.Path, sourceDir & fld.Name
      End If
    Next
  End If

End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.