Basic Scripting with VBScript

Scripts can be used to simplify system administration jobs on the computers.

Scripting is used to automate tasks, that otherwise would need to be done manually. Scripts are plain text and not compiled, and therefore need some other program to run them. They are also therefore easy to view and modify and can be written using any basic text editor such as notepad.

 

Basic Rules

VBScript is not case sensitive, such that

wscript . echo

Is interpreted exactly the same way as:

WsCrIpT.EcHo

Although proper use of case will increase the readability of your code. Extra white space is ignored, such that

WScript.Echo “Hello”

Is the same as:

WScript.    Echo “Hello”

This can be used to make the script more readable.

Long lines may be split using the _ (underscore) character

For example

WScript.Echo  “This is a very long line so it needs to be”_
& “ split so it can be viewed without
scrolling”_  & “ sideways”

Commenting

Comments can be written in the script, and it is advisable to do so that when the script is looked at after a period of time, you still know what it does. Comments are written after an apostrophe, and continue until the end of the line, like so:

‘This script will display the word hello
WScript.Echo “Hello”    ‘display hello

Variables

Variables are placeholders for values. All variables in VBScript are of the type variant, which means that they can hold anything, from numerical to string and date to complex objects. Variables do not need to be declared, but they can be. It is possible to force declaration of variables by including the command at the top of a script-

Option Explicit

Turning on Option Explicit helps with debugging, as often misspelled variable names are a hard to spot error, which may not be detected otherwise.
Variables can be declared and used thus,

Dim myNumber myNumber = 30 
WScript.Echo myNumber 
myNumber = myNumber + 10 
WScript.Echo myNumber

Strings

Strings are sequences of characters, they must be enclosed in double quotes.

Dim myString 
myString = “Hello” 
WScript.Echo myString 
myString = myString & “ there!” 
WScript.Echo myString

Notice that + is used for adding numbers, and & is used for concatenating strings.

Constants

Constants are like variables in that you assign a name to a value; however constants, once declared can not change their value. They are useful for values that are used multiple times in a script, and may need to be changed at some point in the future (but not by the script).
Constants are declared like this:

Const Pr = 3.1415927

Arrays

Arrays are variables that can hold multiple values. They are declared similarly to ordinary variables, except they have a number in brackets after them. This number is the upper bound of the array, or the maximum index you can put something in, and is one less that the maximum number of elements you want the array to hold.
They values in the array are accessed by specifying the index of the value you want (in the range from 0 to the upper bound), like this:

Dim myArray(2)
myArray(0) = 12
myArray(1) = 7
myArray(2) = myArray(0) + myArray(1)
WScript . Echo myArray(2)

Array Functions

An easy way to initialise all the values in an array is to use the Array statement,

myArray = Array(10, 20, 67, 1)

The array will then contain the four numbers, 10, 20, 67 and 1, and the upper bound of the array will be 3.
Arrays can be emptied using Erase,

Erase myArray

To find the upper bound of an array, use the UBound function

Dim max
max = UBound(myArray)

Objects

Objects are packages of code, which can be used without knowing how they work. Just know that the code can be used, and will do something, or produce an output that is defined, but not know the exact mechanism by which it works. The object might also have properties. These are variables that belong to the object.

Objects are created, assigned to a variable, and then their methods and properties can be used

A commonly used object is the FileSystemObject.

Cons C ForReading = 1
Dim objFSO ‘Declare variables
Dim objTextFile
Dim strResponses
Set objFSO = CreateObject(”Scripting.FileSystemObject”) Set objTextFile = objFSO.OpenTextFile — (“c: \scripts \ping response. txt”, ForReading)
strResponses = objTextFile.ReacL4ll
WScript . Echo strResponses
obj Text File. Close

In this example, a FileSystemObject is created. The method OpenTextFile of the FileSystemObject is used to open the specified text file. The method ReadAll of the TextStream object is then used to put the entire contents of the file in a String variable, which is then outputted to the screen. The text file is then closed using the Close method of the TextStream object.

Another object you have already seen is the WScript object, which has the method Echo.

Conditional Statements

Conditional statements are used to control the flow of a program.
The If. . . Then statement is used to make something happen when a condition is met, for example:

Dim myValue
myValue = Int(Rnd*10+1) ‘generate random number between 1 & 10 If (myValue >= 5) Then
WScript.Echo “Greater than or equal to 5”
End If

In this example, a random integer between 1 and 10 is computed. If the number is greater than or equal to 5, the message “Greater than or equal to 5” is outputted to the screen.
This can be extended using the Else statement, to include a message when the number i less than 5.

Dim myValue
myValue = Int(Rnd*10+1) ‘generate random number between 1 & 10 If (myValue >= 5) Then
WScript.Echo “Greater than or equal to 5”
Else
WScript. Echo “Less than 5”
End If

This can be extended further, using the ElseIf statement, to cause a three (or more) possible options.

Dim myValue
myValue Int(Rnd*l0+l) ‘generate random number between 1 & 10 If (myValue) > 5) Then
WScript.Echo “Greater than or equal to 5”
Elseif my Value = 2
WScript . Echo “2”
Else
WScript.Echo “Less than 5 and not 2”
End If

When there are a large number of tests to perform on the same variable, it is often easier to use a Select Case statement, for example:

Dim myValue
myValue = Int(Rnd*lO+l) ‘generate random number between 1 & 10
Select Case myValue
Case 2
WScript . Echo “2”
Case 5
WScript.Echo “5”
Case 9
WScript . Echo “9”
Case Else
WScript.Echo “Not 2, 5 or 9”
End Select

Looping

Looping is another flow control mechanism, which allows the same code to be repeated.
For.. .Next loops are useful when you need to execute code a specific number of times. For example, this code has a For.. .Next loop which counts backwards from 10.

Dim count
Forcount=loToOStep-l
If count > 0 Then
WScript Echo count
Else
WScript.Echo “Blast Off!”
End If
Next

The variable count is set to 10 when the ioop starts, and each repetition of the loop subtracts 1 until 0 is reached.
When you have a collection of objects, or an array, you can use For Each.. .Next loops, like this example:

Dim items()
Dim item
items = Array(1,76,3,12,8,5,53,2)
For Each item In items
If (item / 2) = Int(item/2) Then
WScript.Echo item & “ is divisible by 2”
Else
WScript.Echo item & “ is not divisible by 2”
End If
Next

Do.. .Loop is another useful looping structure for when then amount of times the loop needs to be executed is not known at the start of the loop.
The ioop executed continuously while a condition is true, or until a condition happens, as in the next example showing the different forms a Do.. .Loop can take.

Dim sum
Dim number
number = 1
sum = 0
Do While sum <= 100 sum = sum + number
number = number + 1 Loop
WScript.Echo sum WScript . Echo number number = 1 sum = 0
Do Until sum >100
sum = sum + number<
number = number + 1 Loop
WScript.Echo sum WScript. Echo number number = 1 sum = 0
Do
sum = sum + number
number = number ÷ 1
Loop While sum <=100
WScript.Echo sum
WScript . Echo number
number = 1
sum = 0 Do
sum = sum + number
number = number + 1
Loop Until sum >100
WScript.Echo sum
WScript . Echo number

Running Scripts

The simplest way to run a VBScript is to double-click on it. This will launch the program
WScript, which runs the script. There is also a DOS-based version of WScript called
CScript.