'=======================================================
'
' Script: VBScript Backup & Sync
' Date: 04/17/2006
' Author: Michael J. Damato
' Contact: mjdamato@cox.net
'
' Notes: This is still in development
'=======================================================
Option Explicit
'Create needed variables
Dim objNetwork, objFSO, RegExObj, variablesObj
Dim SOURCE, TARGET, FLDR_EXcLUDE, FILE_EXCLUDE
Dim replicate, synchronize, silent, hiddenFiles, hiddenFldrs
Dim sourceObj, subSourceObj, sourceFileObj, sourceFile, newSource
Dim targetObj, subTargetObj, targetFileObj, targetFile, newTarget
Dim WshShell, sUserName, WshEnv
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objNetwork = wscript.CreateObject("wscript.network") 'objNetwork.Username
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set variablesObj = WScript.Arguments
Set WshEnv = WshShell.Environment("Process")
'=======================================================
'=============== USER DEFINED VARIABLES ================
'=======================================================
sUserName = WshEnv("%USERPROFILE%") 'Could use sUserName = WshNetwork.UserName
SOURCE = sUserName & "\My Documents"
TARGET = "D:\My Documents"
'Excluded folders
'- list folder names, comma separated
FLDR_EXcLUDE = ""
'Excluded file types
'- list file extensions (without periods), comma separated
FILE_EXCLUDE = "" 'exe,txt
'****************************************************************************************************
'Replication
'1. Starts with source folder and if it doesn't exist on the target copies it and all contents there
'2. If the source folder does exist on the target it checks the files in that folder and copies any missing files or files with later modified dates to the target
'3. Recurses through all subfolders (all levels) in the source, repeating steps 1 & 2 for each
'Synchronization
'4. Starts with target folder and if it does not exist in the source deletes it.
'5. If the target folder does exist in source it goes through all files in the target folder and deletes them if they do not exist in the source
'6. Recurses through all sub folders (all levels) in the target, repeating steps 4 & 5 for each.
'****************************************************************************************************
replicate = true 'Perform replication: true/false
synchronize = false 'Perform synchonization: true/false
silent = false 'Show dialogs: true false
hiddenFldrs = true 'Process hidden folders: true/false
hiddenFiles = true 'Process hidden files: true/false
' NOTE: If a folder is missing in the target, the entire
' folder is copied, even the hidden files
'=======================================================
'Use variables passed from command line if sent
If WScript.Arguments.Count > 0 Then
replicate = variablesObj(0)
End If
If WScript.Arguments.Count > 1 Then
synchronize = variablesObj(1)
End If
If WScript.Arguments.Count > 2 Then
silent = variablesObj(2)
End If
'Set up regular expression object
Set RegExObj = New RegExp
RegExObj.IgnoreCase = True
FILE_EXCLUDE = "," & FILE_EXCLUDE & ","
FLDR_EXCLUDE = "," & FLDR_EXCLUDE & ","
'Verify if source folder exists
If Not (objFSO.FolderExists(SOURCE)) Then
'Do not replicate, Perform error handling
If Not ( silent ) Then
MsgBox "Source Missing"
End If
'Proceed if replicating is on
ElseIf ( replicate ) Then
replicateFolders SOURCE, TARGET
End If
'Verify if target folder exists
If Not (objFSO.FolderExists(TARGET)) Then
'Do not synchronize, Perform error handling
If Not ( silent ) Then
MsgBox "Target Missing"
End If
'Proceed if synching is on
ElseIf ( synchronize ) Then
syncFolders SOURCE, TARGET
End If
'Cleanup variables
Set objFSO = nothing
Set objNetwork = nothing
Set variablesObj = nothing
Set RegExObj = nothing
Set sourceObj = nothing
Set targetObj = nothing
Set targetFileObj = nothing
Set sourceFileObj = nothing
If Not ( silent ) Then
MsgBox "Backup Complete!", 0, "Done"
End If
'=======================================================
Sub replicateFolders (currentSource, currentTarget)
'=======================================================
'This procedure will call the procedure replicateFiles
'to process the files in the current directory. Then it
'will check the subfolders in the current directory. If
'a sub directory does not exist in the target it will be
'copied there. If it does exist the procedure will
'perform replicateSource on that directory as well
'=======================================================
'Check if target folder exists
If Not (objFSO.FolderExists(currentTarget)) Then
'Create the target folder and copy the contents of currentSource there
objFSO.CreateFolder(currentTarget)
objFSO.CopyFolder currentSource, currentTarget
Else
'Create folder object of the currentSource
Set sourceObj = objFSO.GetFolder(currentSource)
'Perform file replication for the currentSource
replicateFiles sourceObj, currentTarget
'Do recursive processing of sub folders
For Each subSourceObj in sourceObj.SubFolders
If Not excluded(subSourceObj.Name,0,FLDR_EXCLUDE) Then
If (hiddenFldrs) Or (Not subSourceObj.Attributes And 2) Then
newTarget = TARGET & RIGHT(subSourceObj.Path,(Len(subSourceObj.Path)-Len(SOURCE)))
replicateFolders subSourceObj.path, newTarget
End If
End If
Next
End If
'=======================================================
End Sub 'replicateFolders
'=======================================================
'=======================================================
Sub replicateFiles (sourceObj, targetPath)
'=======================================================
' Recurse through all files in the source object
For Each sourceFileObj in sourceObj.Files
If Not excluded(sourceFileObj.Name,1,FILE_EXCLUDE) Then
If (hiddenFiles) Or (Not sourceFileObj.Attributes And 2) Then
targetFile = targetPath & "\" & sourceFileObj.Name
'Check if file exists in target
If objFSO.FileExists(targetFile) Then
'Create file object of target file
Set targetFileObj = objFSO.GetFile(targetFile)
'Check if file in source is newer
If sourceFileObj.DateLastModified > targetFileObj.DateLastModified Then
'Overwrite existing file in target with newer source file
sourceFileObj.Copy targetFile, true
End If
Else
'Copy file from source to target
sourceFileObj.Copy targetFile
End If
End If
End If
Next
'=======================================================
End Sub 'replicateFiles
'=======================================================
'=======================================================
Sub syncFolders (currentSource, currentTarget)
'=======================================================
'Check if source folder exists
If Not (objFSO.FolderExists(currentSource)) Then
'Delete the current target folder
objFSO.DeleteFolder currentTarget
Else
'Create folder object of the currentTarget
Set targetObj = objFSO.GetFolder(currentTarget)
'Perform file synching for the currentTarget
syncFiles currentSource, targetObj
'Do recursive processing of sub folders
For Each subTargetObj in targetObj.SubFolders
If Not excluded(subTargetObj.Name,0,FLDR_EXCLUDE) Then
If (hiddenFldrs) Or (Not subTargetObj.Attributes And 2) Then
newSource = SOURCE & RIGHT(subTargetObj.Path,(Len(subTargetObj.Path)-Len(TARGET)))
syncFolders newSource, subTargetObj.path
End If
End If
Next
End If
'=======================================================
End Sub 'syncFolders
'=======================================================
'=======================================================
Sub syncFiles (sourcePath, targetObj)
'=======================================================
'=======================================================-
' Recurse through all files in the target object
For Each targetFileObj in targetObj.Files
'Do not process excluded file types
If Not excluded(targetFileObj.Name,1,FILE_EXCLUDE) Then
sourceFile = sourcePath & "\" & targetFileObj.Name
'Delete target file if does not exist in source
If Not (objFSO.FileExists(sourceFile)) Then
targetFileObj.Delete true
End If
End If
Next
'=======================================================
End Sub 'syncFiles
'=======================================================
Function excluded (itemName,isFile,excludeList)
'=======================================================
' Test if itemName is in excluded list
'=======================================================-
'If file, get the file extension
If isFile Then
itemName = Right(itemName,Len(itemName)-InStrRev(itemName,"."))
End If
RegExObj.Pattern = "," & itemName & ","
excluded = RegExObj.Test(excludeList)
'=======================================================
End Function 'excluded
'=======================================================