VBScript
VBScript is short for Microsoft’s Visual Basic Scripting Edition. It is most commonly used in Microsoft’s “classic” Active Server Pages (ASP) and for client-side scripting in Microsoft’s Internet Explorer. VBScript is a subset of Visual Basic for Applications (VBA), and its similarities to VB and VBA allow developers who are familiar with these languages to quickly learn and use VBScript. Unlike compiled Visual Basic code, VBScript is interpreted at runtime. This slows down performance but offers great flexibility because VBScript can be deployed quickly and easily as plain text. The current version of VBScript is 5.0, which has been stable for quite some time.

Microsoft has implemented the VBScript language in a variety of "hosts", the most common being Internet Explorer (browser scripting), Internet Information Server (Active Server Pages), Outlook (email automation), and the Windows Script Host (scripting within Windows). Active Server Pages is by far the most frequently used VBScript host. The Windows Script Host object model provides a logical, systematic way to perform many administrative tasks such as network administration, application development and deployment, and automation of personal tasks. You may encounter VBScript in a variety of other hosts from Microsoft and other companies.

Besides its portability, VBScript is also powerful because most hosts enable the usage of any of the thousands of ActiveX/COM components that can be purchased commercially or custom built. Using VBScript and external components like ADO, the Scripting Runtime, or components you build yourself, you can do just about anything that can be done with Microsoft Windows.

Fast Facts
These basic but necessary facts, that are easy to forget if you haven't worked with the technology for a while, are included on the first page of each chapter.
Basics
VBScript is a subset of Visual Basic for Applications, so many of the same rules for VB apply to VBScript, including the structure of variables and procedures. Although VBScript has only one data type (the Variant), each of the VB data types is represented as a 'subtype' of the Variant type. With a few exceptions (like ASP include files), the code of a VBScript program is usually contained in a single file. In some hosts (like client-side browser scripting) VBScript also supports VBA’s event-driven model, with automatic event handler procedures created by combining the control name and the event name, with an underscore between the two.

Although VBScript is used in coding Outlook forms, etc., this chapter focuses on the three main uses of VBScript: ASP programming, client-side scripting in Internet Explorer, and general Windows scripting and automation.

Built-In Objects
The VBScript engine is contained in a file named vbscript.dll. This file features support for the base VBScript language, and provides a number of built-in objects.

Err The Err object is automatically set with information relating to the most recent error that occurred in the script execution. More can be found about Err in the VBScript Error Handling section of this chapter.

RegExp With VBScript 5.0 engine, VBScript supports the pattern-matching capabilities of regular expressions using the RegExp object. More can be found about RegExp in the Regular Expressions section in this chapter.

Class Object A class is the template for an object. Classes have variables, methods, and properties. When instantiated, these elements are used. Classes have been supported in VBScript only since version 5.0. More can be found about Class in the VBScript Classes section of this chapter.

Debug Object The Debug object cannot be created directly, but it is always available for use. The Write and WriteLine methods of the Debug object display strings in the immediate window of the Microsoft Script Debugger at run time. If the script is not being debugged, the methods have no effect:

    Debug.Write "The value of counter is " & counter

Match Object More can be found about Match Object in the Regular Expression section of this chapter.

Matches Collection More can be found about Match Collection and SubMatches Collection in the Regular Expression section of this chapter.

Windows Script Host
Windows scripting brings simple, powerful, and flexible scripting to the Windows platform. It provides objects for manipulation of script execution, as well as functions for other actions. Using these, we can print messages, map drives, connect to printers, modify registry keys, etc. WSH is built into Microsoft Windows 98, 2000, XP and Millennium Editions. Several areas of functionality have been addressed in the latest version, 5.6.

When running scripts from Windows, WScript.exe provides a Windows-based dialog box for setting script properties. Whether you use WScript.exe (Windows) or CScript.exe (Command Prompt), you still run the scripts in the same manner. The difference is only in the output — WScript generates windowed output, while CScript sends its output to the command window. Windows Script Host offers several objects and constants for interacting with the Windows environment and shell. For information on object model, visit http://msdn.microsoft.com/scripting and click on Windows Script Technologies. The following basic Windows script verifies whether a file has required permissions:

    Dim Signer, File, ShowUI, FileOK
    Set Signer = CreateObject("Scripting.Signer")
    File = "c:\newfile.wsf"
    ShowUI = True
    FileOK = Signer.VerifyFile(File, ShowUI)
    If FileOK Then
         WScript.Echo File & " is trusted."
    Else
         WScript.Echo File & " is NOT trusted."
    End If

This chapter was written by Srinivasa Yeramati and reviewed by Jack Minster.