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

SynchronizeDirectories - Synchronize the contents of two directories

Total Hit ( 2222)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 



Click here to copy the following block
' Synchronize two directories
'
' This routine compares source and dest directories 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 routine and a reference to
'    the Microsoft Scripting Runtime type library

Sub SynchronizeDirectories(ByVal sourceDir As String, ByVal destDir As String, _
  Optional ByVal TwoWaySync As Boolean)
  Dim fso As New Scripting.FileSystemObject
  Dim index As Long
  Dim copyDirection As Integer  ' 1=from source dir, 2=from dest dir,
                  ' 0=don't copy
  
  ' retrieve name of files in both directories
  Dim arr() As Variant
  arr = CompareDirectories(sourceDir, destDir)
  
  ' ensure that both dir names have a trailing backslash
  If Right$(sourceDir, 1) <> "\" Then sourceDir = sourceDir & "\"
  If Right$(destDir, 1) <> "\" Then destDir = destDir & "\"
  
  For index = 1 To UBound(arr, 2)
    ' assume this file doesn't need to be copied
    copyDirection = 0
    
    ' see whether files are
    Select Case arr(1, index)
      Case cdeEqual
        ' this file is the same in both directories
      Case cdeSourceDirOnly
        ' this file exists only in source directory
        copyDirection = 1
      Case cdeDestDirOnly
        ' this file exists only in destination directory
        copyDirection = 2
      Case Else
        If arr(1, index) = cdeAttributesDiffer Then
          ' ignore files that differ only for their attributes
        ElseIf (arr(1, index) And cdeDateDiffer) = cdeSourceIsOlder Then
          ' file in destination directory is newer
          copyDirection = 2
        Else
          ' in all other cases file in source dir should be copied
          ' into dest dire
          copyDirection = 1
        End If
    End Select
    
    If copyDirection = 1 Then
      ' copy from source dir to destination dir
      fso.CopyFile sourceDir & arr(0, index), destDir & arr(0, index), _
        True
    ElseIf copyDirection = 2 And TwoWaySync Then
      ' copy from destination dir to source dir
      ' (only if two-way synchronization has been requested)
      fso.CopyFile destDir & arr(0, index), sourceDir & arr(0, index), _
        True
    End If
  Next
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.