P1: JDT
WL040C-44 WL040/Bidgoli-Vol III-Ch-51 June 23, 2003 16:33 Char Count= 0
OBJECTS INVBSCRIPT 627method of the WSH object model’s WScript object. Ref-
erence to WScript.ScriptFullName returns the variant
(subtype string) property of the WScript object that
contains the full path and file name of the script be-
ing executed. WScript.Arguments.Item(0) returns the
first item in the list of command line arguments used
when the script was invoked (an example of a col-
lection). Object event references are typically used in
user-defined subroutine declarations. Thus, on a Web
page containing an HTML <Select> element (i.e., pull-
down menu) named “myChoice,” a programmer could in-
clude a subroutine declared as “Sub myChoiceOnChange
(),” which would then be invoked each time the user
changed his or her selection from that particular
menu.Intrinsic Object Models
VBScript provides two built-in object models, one intrin-
sic to the language itself, and one included as part of
the Microsoft Scripting Runtime library (and therefore
also available to JScript implementations). The former
includes the regular expression object (RegExp) and the
error object (Err). Note that the RegExp object must be
instantiated using the New key word (as typical for early
binding) rather than the CreateObject method (late bind-
ing) as required for other objects instantiated in VBScript;
thus,set objMyRegularExpression = New RegExpThe RegExp object allows testing of strings for pattern
matches and manipulation of matches found through the
“Match” object collection (a child of the RegExp object
class).
The Err object is singular, is always available, and
therefore does not need to be explicitly instantiated. The
Err object includes a method to clear an error condition
and properties providing the number and description of
the last error that occurred. It is used in conjunction with
the On Error Resume Next statement for user-defined
error handling.
Version 5.6 of the Microsoft Scripting Runtime Library
provides two important objects, the FileSystemObject, for
accessing file system directory information and reading
from and writing to individual text files, and the Dictio-
nary object, which is similar to associative arrays found
in Perl and other programming languages. The FileSys-
temObject is at the top of an extensive and powerful ob-
ject hierarchy that provides access through a variety of
child objects to the host system’s drives, directories, and
files. “Scripting.FileSystemObject” is the ProgID for the
FileSystemObject. Using methods and properties of the
FileSystemObject you can browse drives and folders of
the host computer, create new folders and text files on the
host computer’s file system, and open existing text files
on the host computer for reading and/or writing (e.g., by
creating and using child TextStream objects). Note, when
using the FileSystemObject (and most other external COM
objects) for client-side scripting in Microsoft Internet Ex-
plorer, Internet Explorer typically warns users that use of
these objects might be unsafe. Users can even configureInternet Explorer not to allow instantiation and use of
external COM objects. Listing 4 illustrates the use of the
FileSystemObject in WSH.
Finally, the Dictionary object of the Microsoft Scripting
Runtime Library allows you to store and manipulate lists
of key value pairs, i.e., lists of values where each value is
tied to a unique key. “Scripting.Dictionary” is the ProgID
of the Dictionary object. This object has a small set of
properties and methods that allow your script to add and
remove items from the dictionary list, retrieve or alter in-
dividual values by key, change or replace individual keys,
and check to see if a particular key exists in the dictionary
list.Listing 4:Illustrating the use of FileSystemObject in the con-
text of WSH.Option Explicit
Const conMyLine =_
"This was written by a VBScript script."
Const conMyFileName = "Temp.txt"
Dim objMyFS, objMyTS
Dim strFileName, strTextLine
set objMyFS = WScript.CreateObject_
("Scripting.FileSystemObject")
strFileName = Replace_
(WScript.ScriptFull Name,_
WScript.ScriptName, conMyFileName)if objMyFS.FileExists(strFileName) then
set objMyTS = objMyFS.OpenTextFile_
(strFileName)
strTextLine = objMyTS.ReadLine
objMyTS.Close
MsgBox (strTextLine)
else
set objMyTS = objMyFS.CreateTextFile_
(strFileName)
objMyTS.WriteLine(conMyLine)
objMyTS.Close
MsgBox ("Created" & strFileName)
end ifset objMyTS = Nothing
set objMyFS = NothingWindows Scripting Host Object Model
The WSH object model consists of a top-level object,
WScript, with properties germane to the script and how
it was invoked, methods for suspending and ending script
execution and creating and using external objects, and
a half-dozen immediate child objects, each providing
access to a service or a function of the host system.
WScript child objects include: WshArguments, a collec-
tion containing any command line arguments used when
invoking the script; WshNetwork, providing access to
network configuration and services; StdIn, StdOut, and
StdErr, specialized read-only or write-only (as appro-
priate) TextStream objects providing access to standard
input and output devices (e.g., keyboard and display);
and WshShell, providing access to a variety of Windows