Skip to content

File and Folder Replication with VBScript

This is a VBScript I wrote a while back which was used to synchronize folders on a file server to other servers at remote sites. The source server was where changes have to occur, otherwise they’ll get over-written if made on remote servers.

'=========================
' Name:     file_replication.vbs
' Date:     April 3rd, 2006
' Author:    Cale Dunlap (cale.dunlap@trx.com)
' Purpose:    Copies files from one location to multiple locations based on date information
'=========================

'========================= BEGIN GLOBALS
strLogDestination = "C:\scripts\"     ' Destination for the log file
iNumSites = 2     ' Number of desintation locations
strSourceFolder = "\\atl30559\site1"    ' Source location

Dim arrDestinationFolders(1)     ' Array of destinations
ReDim arrDesintationFolders(iNumSites)    ' Redim the array to the appropriate number

' List of all destination locations
arrDestinationFolders(0) = "\\atl30559\site2"
arrDestinationFolders(1) = "\\atl30559\site3"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFolder = objFSO.GetFolder(strSourceFolder)

'========================= END GLOBALS
''''''''''''''''''''''''''
'========================= BEGIN ROUTINES AND FUNCTIONS
Sub WriteLogEntry( strMessage )
    Set objLogFile = objFSO.OpenTextFile(strLogDestination & "replog.txt", 8, True, 0)
    objLogFile.WriteLine Now() & " :: " & strMessage
    objLogFile.Close
End Sub

Sub Replicate( objSource, objDestination )
    ' Copy the files first
    For Each objFile In objSource.Files
        If objFSO.FileExists( Replace(objFile.Path, objSource.Path, objDestination.Path) ) Then
            Set objFileDest = objFSO.GetFile( Replace(objFile.Path, objSource.Path, objDestination.Path) )
            MinutesOld = DateDiff("n", objFile.DateLastModified, objFileDest.DateLastModified)
            If MinutesOld <> 0 Then
                strSourcePath = objFile.Path
                strDestinationPath = objFileDest.Path
                objFSO.CopyFile strSourcePath, strDestinationPath, True
                WriteLogEntry "Copied " & objFile.Path & " to " & objFileDest.Path & " because the source date modified different from the destination date modified."
            End If
        Else
            strSourcePath = objFile.Path
            strDestinationPath = Replace( strSourcePath, objSource.Path, objDestination.Path)

            objFSO.CopyFile strSourcePath, strDestinationPath, True
            WriteLogEntry "Copied " & strSourcePath & " to " & strDestinationPath
        End If
    Next

    ' Copy the folders now
    For Each objSubfolder In objSource.Subfolders
        If Not objFSO.FolderExists( Replace(objSubFolder.Path, objSource.Path, objDestination.Path) ) Then
            objFSO.CreateFolder Replace(objSubFolder.Path, objSource.Path, objDestination.Path)
            WriteLogEntry "Created folder " & Replace(objSubFolder.Path, objSource.Path, objDestination.Path) & " because it existed on the source, but not the destination."
        End If

        Replicate objSubfolder, objFSO.GetFolder( Replace(objSubFolder.Path, objSource.Path, objDestination.Path) )

    Next

End Sub
'========================= END ROUTINES AND FUNCTIONS
''''''''''''''''''''''''''
'========================= BEGIN MAIN PROGRAM
WriteLogEntry("Script Started.")

' Begin the replication
For Each destFolder In arrDestinationFolders
    Set objDestFolder = objFSO.GetFolder(destFolder)
    Replicate objSourceFolder, objDestFolder
Next

' Notify of completion
WScript.Echo "Complete"
WriteLogEntry("Script completed.")
'========================= END MAIN PROGRAM

Now just slap that in a scheduled task and you’ve got yourself a quick-n-dirty method for file replication.

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

Get Adobe Flash playerPlugin by wpburn.com wordpress themes