SynchroniseDirectories ExitCode, "C:\C++"
................
'******************************************************************************
'* Function: RobocopyExists
'*
'* Purpose: determines whether the program Robocopy can be called from the
'* command line.
'*
'* Input: None
'* Output: boolean - true if Robocopy exists
'*
'******************************************************************************
Function RobocopyExists()
On Error Resume Next
Dim objShell
Dim objExec
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExec = objShell.Exec ("robocopy.exe", 0)
If Err.Number = 0 Then
RobocopyExists = True
Else
RobocopyExists = False
End If
End Function
'******************************************************************************
'* Function: SynchroniseDirectories
'*
'* Purpose: Copies all files created by the previous backup / image
'* to a supplied directory. Uses Macrium environment variables to
'* determine which files to copy.
'*
'* Input: ExitCode - The exit code of the last backup
'* strBackupDirectory - Directory to copy to
'*
'******************************************************************************
Function SynchroniseDirectories(Byval ExitCode, Byval strSyncDirectory)
Dim objShell
Dim objWshProcessEnv
Dim strEnvPrefix
Dim strBackupDirectory
Dim strCmdLine
Dim iReturnCode
Dim fs
Dim objSyncFiles
Dim objBackupDirectory
Dim objSyncDirectory
Dim objBackupFile
Dim objSyncFile
Dim strExtension
Dim dateBackupFile
Dim dateSyncFile
' Only copy files if backup was successful
if ExitCode <> 0 Then
Exit Function
End If
Set objShell = WScript.CreateObject("WScript.Shell")
Set objWshProcessEnv = objShell.Environment("VOLATILE")
' Get the prefix for the last backup set
strEnvPrefix = objWshProcessEnv("MACRIUM_PREFIX")
' Get the directory where we just created a backup
strBackupDirectory = objWshProcessEnv(strEnvPrefix + "_DIRECTORY")
If right(strBackupDirectory, 1) = "\" Then strBackupDirectory = left(strBackupDirectory, len(strBackupDirectory)-1)
If right(strSyncDirectory, 1) = "\" Then strSyncDirectory = left(strSyncDirectory, len(strSyncDirectory)-1)
If RobocopyExists Then
strCmdLine = "robocopy ""<SOURCE>"" ""<DESTINATION>"" *.mr* /copy:DAT /lev:0 /purge /r:0"
Else
' Robocopy does not exist - using xcopy
Set fs = CreateObject("Scripting.FilesystemObject")
Set objBackupDirectory = fs.GetFolder(strBackupDirectory)
Set objSyncDirectory = fs.GetFolder(strSyncDirectory)
For Each objSyncFile in objSyncDirectory.Files
strExtension = fs.GetExtensionName(objSyncFile)
dateSyncFile = objSyncFile.DateLastModified
If Left(strExtension,2) = "mr" Then
' Check if file has been deleted
If Not(fs.FileExists(strBackupDirectory+"\"+objSyncFile.name)) Then
fs.DeleteFile(objSyncFile)
Else
' Check if file has been modified
Set objBackupFile = fs.GetFile(strBackupDirectory+"\"+objSyncFile.name)
dateBackupFile = objBackupFile.DateLastModified
If DateDiff("m", dateBackupFile, dateSyncFile) <> 0 Then
fs.DeleteFile(objSyncFile)
End If
End If
End If
Next
strCmdLine = "xcopy ""<SOURCE>\*.mr*"" ""<DESTINATION>"" /c /d /h /i /v /y"
End If
strCmdLine = Replace(strCmdLine,"<SOURCE>", strBackupDirectory)
strCmdLine = Replace(strCmdLine,"<DESTINATION>", strSyncDirectory)
iReturnCode = objShell.Run(strCmdLine,0,true)
if iReturnCode <> 0 then
' Handle synchronisation error
else
' Everything OK
end if
' Clean up
Set objShell = nothing
Set objWshProcessEnv = nothing
End Function
|