Knowledgebase

v4: Using VBScript to delete differential images

Expand / Collapse
 

v4: Using VBScript to delete differential images



This article shows you how to use VBScript to delete differential images in a backup set. This enables you to retain a specified number of recent differential images.


In the following example we will generate a VBScript source file that creates a differential image and then deletes differentials in the same image set retaining the '4' most recent differential images.

  1. Right click on your differential XML definition and select 'Generate a VBScript file'.

  2. Accept all the defaults and click 'OK'
  3. Click the VBScript tab, right click on the new VBScript list entry and select 'Edit'. Notepad will open.



    Make sure you turn off 'Word Wrap' as this makes editing a lot easier.




  4. Edit the file in Notepad as follows:

After the line:

ExitCode = Backup ("""C:\Program Files\Macrium\Reflect\reflect.exe"" -e -w ""PATH TO XML""")



Copy and paste the following:


DeleteDifferentialFiles ExitCode, 4



Note: The number '4' above is the number of recent differential files you want to keep in the backup set


At the very *end* of the file copy and paste the following code:


'******************************************************************************
'* Function: DeleteDifferentialFiles
'*
'* Purpose:  Deletes all but most 'n' recent differential file(s).
'*       
'*           Uses Macrium environment variables to
'*           determine which files to delete.
'*
'* Input:ExitCode                    -The exit code of the last backup
'*           NumberOfDifferentialsToKeep - The number of differentials to retain
'*
'******************************************************************************
Function DeleteDifferentialFiles(Byval ExitCode, Byval NumberOfDifferentialsToKeep)
Dim objShell
Dim objWshProcessEnv
Dim strEnvName
Dim nFiles
Dim Count
Dim strEnvPrefix
Dim strCurrentDifferentialNumber
Dim strFileToDelete    
Dim objFS

' Only delete  files if backup was successful
if ExitCode <> 0 Then
Exit Function
End If
Set objFS = CreateObject("Scripting.FileSystemObject")
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 total number of files for the last backup set
nFiles = CInt(objWshProcessEnv(strEnvPrefix + "_FILECOUNT"))
' Get the last differential number. Note: differential 0 is the full backup
strCurrentDifferentialNumber   = objWshProcessEnv(strEnvPrefix + "_CURRENT_INCREMENT")
for Count = 1 To nFiles
        ' Construct the environment variable name for the bckup method
        ' 0 = Full, 1 = Incremental, 2 = Differential
        strEnvName = strEnvPrefix + "_METHOD" + CStr(Count)
        ' Only delete differential files
        if CInt(objWshProcessEnv(strEnvName)) = 2 Then
       ' Construct the environment variable name for the differential for eachfile
strEnvName = strEnvPrefix + "_INCREMENT" + CStr(Count)
        ' Don't delete the current differential 
if CInt(objWshProcessEnv(strEnvName)) < ( CInt(strCurrentDifferentialNumber) -NumberOfDifferentialsToKeep + 1) Then
            ' Construct the environment variable for the full filepath
    strEnvName = strEnvPrefix + "_FILEPATH" + CStr(Count)
            strFileToDelete = objWshProcessEnv(strEnvName)
    ' Delete the file if itexists           
            If objFS.FileExists(strFileToDelete) Then
           ON ERROR RESUME NEXT
               objFS.DeleteFile strFileToDelete
    End If 
End If  
 End If
Next

' Clean up
Set objShell = nothing
Set objWshProcessEnv = nothing
Set objFS = nothing
End Function


5. Save the file. You can run VBScript files like XML files and you can schedule the VBScript file to run by right clicking on the file and selecting 'Schedule'.



Note
: You should only run this script on *differential* backup sets.





    Related Links




    .

    Details
    Last Modified:Friday, July 15, 2011

    Last Modified By: jon.dittman@macrium.com

    Type: Tutorial

    Article not rated yet.

    Article has been viewed 5,399 times.

    Options