Subscribe to Windows IT Pro
July 01, 1999 12:00 AM

Scripting 101, Part 2

Windows IT Pro
InstantDoc ID #5505
Rating: (1)
Downloads
5505.zip

Declaring and initializing variables
In last month's column, I discussed the structure of a script. I divide scripts into three sections: declarations and initializations, the body of the script, and function and subroutine definitions. This month, I continue my journey into the fundamentals of scripting by examining the declaration and initialization section in detail. Before we start, you need to know some VBScript basics to ease your transition to Windows Scripting Host (WSH).

VBScript Basics
As with any other programming or scripting language, VBScript has some general ground rules that apply to every script. At first glance, these rules might not appear to be significant, but knowing these language tidbits can often shave a few minutes off a frustrating debugging exercise.

The first ground rule is that VBScript isn't case-sensitive. For example, declaring a variable as strTempFile and later referencing it as STRtEMPfILE is perfectly legal. Likewise, VBScript statements, function names, and subroutine names are case-insensitive. Despite this flexibility, I encourage you to pick a case variation that suits your needs and stick with it.

Although VBScript is case-insensitive, some situations (e.g., comparing two string values) require you to be case-conscious. You also need to be case- conscious when you use Active Directory Service Interfaces (ADSI) namespace identifiers (e.g., LDAP to specify Lightweight Directory Access Protocol, WinNT to specify Windows NT, NDS to specify NetWare 4.x, NWCOMPAT to specify NetWare 3.x). You must key ADSI namespace identifiers according to the ADSI specification, or a runtime error will occur when your script attempts to bind to an ADSI namespace. (For more information about ADSI namespace usage, read the Active Directory Programmer's Guide available at http://msdn.microsoft.com/ developer/windows2000.)

The second VBScript rule is that you can use either an apostrophe (´) or a Rem statement to include comments in a script. I use both styles in Listing 1's demo.vbs script, page 160. You can insert comments on a separate line or at the end of a line of code, but you can't insert comments at the end of a code line that contains a line-continuation marker (_).

A third rule is that VBScript doesn't care about extra white space. Adding extra spaces or blank lines can often improve the code's readability. VBScript ignores the blank lines and the extra spaces between the variable name and the assignment operator (=).

The fourth rule is that although VBScript doesn't impose a maximum line length, breaking long lines into multiple short lines can improve the readability of a script. In VBScript, you can use the line-continuation marker to let a statement or line of code span multiple lines. I often use the line- continuation marker when I initialize dynamic arrays (e.g., at callout A in Listing 1) or to pass a long string to WScript's Echo method (WScript.Echo), which I demonstrate several times in the body of the demo.vbs script. When you use the line-continuation marker, you need to include one space immediately before the underscore, or you will encounter an error with some statement types.

On the flip side of rule 4 is the fifth VBScript ground rule, which lets one line of code contain multiple statements. In VBScript, you can use a colon (:) to separate multiple statements within a line of code. In the code line

  Dim strText: strText = "WSH ROCKS!" : WScript.Echo strText  

statement 1 is Dim strText, statement 2 is strText = "WSH ROCKS!", and statement 3 is WScript.Echo strText. Although VBScript supports this capability, I generally don't use it.

The sixth VBScript rule is that you must terminate each line of VBScript code with a new-line character. Pressing Enter automatically inserts the new- line character. VBScript doesn't use the visible line terminator (;) that JScript and Perl use.

The seventh and final ground rule in this month's lesson governs VBScript identifiers (e.g., variables, constants, function names, subroutine names). Identifiers can't exceed 255 characters in length and must begin with an uppercase or lowercase letter of the alphabet.

Bob's Declaration and Initialization Section
The primary elements that make up my declaration and initialization section are script directives, variable declarations, constant definitions, and variable initialization. All these elements (with the possible exception of initialization) are optional. However, using these language features can help you produce scripts that are easier to debug and maintain.

VBScript directives. VBScript includes two directives or statements that significantly affect the runtime behavior of scripts. The two directives are the Option Explicit statement and the On Error Resume Next statement.

The Option Explicit statement requires that you declare (i.e., define) all script variables via a VBScript Dim statement before you use the variables in an expression. When you use Option Explicit, the declaration requirement applies throughout the entire script, including variables in the main body of the script and variables in user-defined functions and subroutines. You must include the Option Explicit statement before any other statements in your script. Therefore, you always find Option Explicit at the top of scripts that use it. The benefit of using Option Explicit is that an error occurs whenever the VBScript interpreter encounters an undefined variable during script execution. Using Option Explicit helps you isolate typographical mistakes and variables with incorrect scopes (a scope defines the visibility of a variable to other parts of the script such as functions and subroutines).

The On Error Resume Next statement controls how a script notifies you when a script encounters a runtime error. When a script runtime error occurs and the script doesn't use the On Error Resume Next statement, WSH displays a dialog box containing the execution error information and aborts the script. Screen 1 shows an example WSH Script Execution Error dialog box. Including an On Error Resume Next statement in a script effectively tells VBScript to continue execution despite a runtime error: VBScript's built-in Err object is set with the corresponding error information, and the script continues. The On Error Resume Next statement lets you trap errors and respond to them programmatically. You can choose to display a friendly, descriptive message or write the error information to a log file. Screen 2 shows an example customized error dialog box.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Feb 22, 2005

    gooooooooood

  • Anonymous User
    7 years ago
    Jan 10, 2005

    Learn HTML first

  • Arnold Schoenberg
    13 years ago
    Dec 13, 1999

    My employer mandates that Perl is our scripting language. I noted that all of the articles in this series assume that VB is the scripting language, but this is not acceptable here. Is it possible to use Perl to create WSH scripts?

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.