Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Jul 14, 2001

SynchronizeDirectories - Synchronize the contents of two directories

' 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

Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap