How to use VBScript with Macrium Reflect


In part four of our series on working with Macrium Reflect, we look at using VBScript to handle a monthly backup cycle. I’ll show you how to generate a template VBScript source file and modify a couple of lines to enable selective execution of the XML definition files created in the previous tutorials.


So far we’ve created full and incremental image definitions and scheduled these to run in a weekly backup cycle. We’ve also used the disk space management option to ensure that our backup disk doesn’t overflow. But, what if you want to schedule your images using a monthly calendar cycle? Say you want to run a full image on the first Monday of every month and an incremental image for all other weekdays. How do you do that?

Macrium Reflect uses the standard Windows Scheduler to schedule all backups and images. This has the advantage of being a very well tried and tested scheduler, plus it reduces valuable program overhead by using an existing, running service. However, while the Windows Scheduler is great for repeating weekly pattern of days, it can’t handle monthly exclusions very well. In our above example we can schedule the full image to run on the first Monday of each month without a problem, but the incremental images would have to be scheduled for every weekday. This means that on the first Monday of each month you’d run both the full and incremental images. This isn’t a very elegant solution; so what do we do?

The answer is VBScript. With VBScript we have total flexibility to do anything we want. We can also reduce the number of tasks to schedule to just one.

If you’re unfamiliar with VBScript source, it can look rather daunting. The syntax seems unnecessarily complex and verbose. However, one thing to remember is that in most cases you can just copy existing code examples and modify them slightly to make them work for you. Macrium Reflect generates a great working template for you to modify, and I’ll show you a couple of simple code lines to add to achieve the scheduling scenario I’ve outlined above.

OK, let’s begin…

  1. Start Reflect, click the ‘XML Definitions’ tab, select the ‘C Full Image’ XML file and then click the ‘Generate VBScript’ button.



  2. The VBScript generation dialog is shown. Modify the VBScript file name to ‘Monthly Schedule’, and then click OK.



    This will generate a VBScript template file using the default options. The defaults will simply run the full image and create a log file in the directory where the XML definition is stored. A VBScript source file can be scheduled in exactly the same way as an XML file, but first we’re going to modify it to run both the full and incremental images on the correct days.

  3. Click the ‘VBScript files’ tab. You should see your new VBScript source file in the list of source files. If you select it with your left mouse you can see the automatically generated code in the lower half of the window.



    This view has the advantage of syntax highlighting. This means that keywords, conditions and certain variables are displayed different colors, making viewing the source (and potential errors) much easier.

  4. Right click on the VBScript file and select ‘Edit’.



    This will open the source file in the default editor, Notepad.exe. Make sure that Word Wrap is turned off. This makes editing the source code much easier.



    Locate the following line of code:



    And replace it with:



    Note: Your XML file paths will be different to the paths above

    A quick summary of the functions used above:

    The Date keyword is the current system date.

    The Weekday function returns the day of the week as a number. 1 = Sunday, 2 = Monday etc.

    DateAdd simply subtracts 7 days from the current date. The “d” means that we are subtracting (or adding) days. The other possibilities are, “yyyy” - Year, “q” – Quarter, “m” – Month, “y” - Day of year, “w” – Weekday, “ww” - Week of year, “h” – Hour, “n” – Minute, “s” – Second.

    The Month function returns the month (1-12) of the DateAdd result and compares this with the current month using the ‘<>’ not equal to operator.



    So, if the current day is Monday and the Monday of last week is a different month, then this must be the first Monday of the current month. As you can see, the code then simply executes the XML definition file for the full image otherwise the incremental XML definition is executed.

    When you’ve made the changes, save the file and close Notepad.

  5. All that remains is to schedule the VBScript file to run every weekday.

    Right click on the VBScript file in the list and select ‘Schedule’. You’ve been through this wizard before, so I won’t go over it again. You could of course, select only weekdays or every day, but the important thing to understand is that whatever you choose, it’s the VBScript code that decides whether to run the full or incremental image not the scheduler.

You’ve done it! VBScript code that modifies the running order of your images. VBScript gives you complete flexibility for any situation you can think of.

This is just a very small example of the power of VBScript. If you want to delve further into VBScript, have a look at the options in the generator. It generates separate functions for each option and provides some handy comments to help you understand the code. An excellent reference of VBScript functions can be found at http://www.w3schools.com/VBscript/vbscript_ref_functions.asp . Here you’ll find, not just the date functions introduced in this article, but every function available to you.

In the next tutorial we’ll imagine that the worst has happened and your PC has been infected with a killer virus. Windows won’t start! How do you get your system back? Don’t panic, I’ll reveal all.



About Annette McGrath

Annette is a senior programmer at Paramount Software UK Ltd. She has been responsible for much of the coding and design of Macrium Reflect and has many years experience in software design and C, C++ programming languages.


For more information about Macrium Reflect visit http://www.macrium.com.