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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date