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.
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.
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.
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.