devxlogo

SynchronizeDirectories – Synchronize the contents of two directories

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 librarySub 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    NextEnd Sub

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist