VBScript sees all four of these data types as subsets of a larger data type, type variant, which can contain any kind of data. Thus, you don't have to tell VBScript what type of data you're feeding it, but you also can't perform some tasks possible with languages such as Visual Basic (VB), which treat these four data types differently. (Don't worry too much about any such limitationsyou might never run into them.) Groups of like data treated together are called arrays.
To simplify data manipulation, VBScript supports two other data types that have no starting value (i.e., they are null) but are assigned one in the course of a script: variables and constants. Variables can (and frequently do) change value in the course of the script while retaining the same name. Constants have one value for the duration of the script and can't be changed.
You can make data available to scripts in several ways. One way is to hard-code the data in the script. For example, coding ""\\bigserver\sharedfolder"" is a valid way of including path information in a script. Another way is to have the person running the script explicitly provide the data or provide input that tells the script to use certain data. The script can also create its own data (e.g., by calculating the date 2 weeks from today's date), then work with it.
One way to manipulate data is with operators, symbols typically used to represent mathematical functions. Some operators take precedence over others, so although VBScript typically reads lines in a script from left to right, operator precedence affects the order in which the operators are interpreted. An expression is a calculation that might include any combination of numbers, variables, strings, or constants to get some result. Expressions can include operators. For example, the expression dInputDate + 2 = dNewDate increments the variable dInputDate by 2, then assigns the new value to the variable dNewDate.
Functions and Subroutines
VBScript provides built-in functions, which let you perform certain operations without having to spell them out in expressions. With these built-in functions, you can manipulate numbers, strings, dates and times, and arrays. VBScript also has conversion functions that let you convert one type of data to another. For example, VBScript typically assumes that a number such as 45 should be the number data type, but you might want to change it to the string data type.
VBScript lets you create user-defined functions (UDFs) to perform an action that you specify in the code. For example, in
Function TestFunct
TestFunct = Sqr(9) + 2
End Function
the TestFunct UDF uses the built-in Sqr function to find the square root of 9, then adds 2 to the result. You could also create a UDF that acts on arguments you feed it, just as the built-in Sqr() function accepts numbers as arguments and derives their square roots.
The TestFunct UDF produces a result that you can return to the main body of the script. A subroutine performs a predefined action that doesn't return a result to the script. The scriptwriter might put the action in a subroutine because it needs to happen many times in the course of the script and he or she doesn't want to have to retype it each time. The subroutine
Sub AskUserName
WScript.Echo _
"Please type a username."
WScript.Quit
End Sub
exploits a couple of capabilities of the WScript object to make it print a string to the screen, then exit the subroutine. Functions and subroutines can use the values of variables declared within the main body of the script or can use variables private to themselves.
Objects in Scripting
Objects represent physical or logical parts of the computing environment, such as disk drives or user account names. You can do simple scripting without resorting to objects, but most administrative scripts use them. If you're using WSH, VBScript can use objects native to WSH, such as those representing files and folders or parts of the registry; VBScript also supports Windows Management Instrumentation (WMI) and Active Directory Service Interfaces (ADSI) objects. WMI objects represent physical and logical parts of the computing system, such as IP addresses, file systems, and network cards. ADSI objects represent resources stored in a directory service such as Active Directory (AD) or other supported directories, such as the Windows NT 4.0 SAM. Static groups of objects of the same ilk are known as classes, and user-defined groups of objects are called libraries.
Objects have properties and methods. Properties describe an object (e.g., IP Address could be a property of the Network Card object, with 12.4.21.197 being the value of that property). Methods are actions you can take against an object (e.g., Copy could be a method of the File object). Not all objects have methods. Properties and methods are both written the same way: object first, a period, then the method or property name (e.g., ObjectName.PropertyName). Objects can contain other objects. For example, the WScript object in WSH contains a subordinate WshArguments object, which is a collection of any input that the person running the script has provided. To refer in a script to the first piece of input in WshArguments, you would type WScript.Arguments(0). As you'll see when we discuss WSH objects, the formal name of the subordinate objects doesn't match the name used to invoke them in a script.
If you're new to scripting, I've probably hit you with enough information for now. In my next column, I'll begin to show you how to apply this information to building administrative scripts.