There are occasions when you need to investigate the VBScript version number. For example, if you have old clients with version 2.x., VBScript commands may not work. To give a specific example you may want a script to pause, in which case you could use WScript.Sleep. The problem is that this .Sleep property was only added in WSH version 5.1. If you are sure all your clients are XP, then no problem, but if the scripts are going to run on Windows 9x machines you may wish to check the version with a simple script.
The first bonus of checking version numbers is that investigation reveals just how many components are involved in a VBscript, for instance, Windows Scripting Host (WSH) and also VBScript itself.
VBScripts need a shell called Windows Scripting Host (WSH). Examples 1 and 2 are designed to extract the version information about WSH. (In Example 3 we switch to the VBScript itself.)
This is a script that will execute equally well on a Windows server or an XP machine.
Instructions for Checking WSH Version
' WSH.vbs
' Sample VBScript to check WSH Version
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - September 2005
' --------------------------------------------------------------------
On Error Resume Next
WScript.Echo "WSH Version: " & WScript.Version
WScript.Quit
Note 1: I am expecting the script to return a WScript version of 5.6
Note 2: Here we have a mixture of literals: "WSH Version: " and variables: WScript.Version. Paying attention to detail pays off. In this instance, notice how the space before the closing quotes makes the output easier to understand.
Note 3: Time and time ampersand (&) is your friend in joining VBScript components. I have to confess that I am often concentrating so hard on the more difficult commands that I forget the ampersand. When I make this sin of omission, WSH brings me up sharply with a message: Code 800A0401 - Expected end of statement.
' WSHname.vbs
' Sample VBScript to check WSH Version
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - September 2005
' --------------------------------------------------------------------
On Error Resume Next
WScript.Echo "WSH Version: " & WScript.Version & " " & WScript.BuildVersion _
& vbcr & "File name: " & WScript.ScriptName
WScript.Quit
Note 1: Most of the time, I only use WScript.Echo and perhaps WScript.Quit, I was surprised to discover that WScript has so many properties, for example .Version, .BuildVersion and even .ScriptName. From a teaching perspective, the message is that objects have properties, which we access with the syntax object.dot.property. Incidentally, my WScript build number was 8827.
Note 2: One potential problem with VBScript commands is that they are more than 80 characters long. Moreover, VBScript has no concept of word-wrap, so if you just continue the same instruction on the next line, VBScript will interpret your intention as two instructions and not one. Fortunately, there is a solution, the underscore (_). What underscore does is tell VBScript that the command has not yet finished, but to read the rest of the same command on the next line.
Note 3: Observe the command vbcr, this makes a carriage return in the actual output message box. It is purely cosmetic, but it often makes output easier to understand.
As we will see our scripts require both VBScript itself and also WSH shell. This script concentrates on the VBScript executable. Incidentally, you can confirm the information by checking the information directly on cscript.exe in the Windows/System32 folder.
Please note that this script checks VBScript and is different from Example 1 and 2 which report version numbers for WSH. You will probably notice how ScriptEngineMajorVersion and ScriptEngineMinorVersion replace WScript.Version.
' VBScript.vbs
' Sample Script to check VBScript Version
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.1 - September 2005
' --------------------------------------------------------------------
WScript.Echo "VBScript Version: " & ScriptEngineMajorVersion _
& "." & ScriptEngineMinorVersion
WScript.Quit
Note 1: Unlike example 1 this script needs to build the version number from two numbers, and artificially concatenate a dot in between.
These three scripts are so short you could easily bolt them all together - could not you? This would mimic real life situations where you build scripts in stages, get them working then bolt them together to create the production script.
From a practical point of view, it may be necessary to check the version of VBScript or WSH. From a pure scripting point of view there are parallels with English. When composing ordinary English, many of us - me included, are unsure when to use a semi-colon; a dash is even harder to master. Syntax is equally important in VBScript, therefore take a minute or two to master the underscore (_) and vbcr.