Section 9.7. Wscript Object

9.7. Wscript Object

The Wscript object is a built-in object. You do not have to create an instance of this object in your scripts; it is created automatically. The Wscript object provides the ability to create new objects, output basic info , and exit a script.

Table 9-7 lists a number of properties the Wscript object exposes.

Table 9-7. Wscript object properties

Full name

Name and path of executable

Interactive

Identifies if script is running in interactive (Wscript) or batch (Cscript) mode

Name

Name of the WSH (e.g., Windows Script Host)

Path

Path where WSH executables reside

ScriptFullName

Full name and path of script

ScriptName

Name of script

Version

Version of WSH


9.7.1. Echo Method

The Echo method outputs data to the screen. If you are running the script using cscript.exe , the information will be output to the command prompt window from which the script was run, while wscript.exe will output the data as a Windows message box. Its syntax is:

 Wscript.Echo [   anyArg1   ,   anyArg2   ...   anyArgX   ] 

9.7.2. CreateObject Method

The CreateObject method creates a new instance of a specified COM object. Once the object has been successfully created, you can access any properties or call any methods the object exposes. Its syntax is:

 Set   objObject   = Wscript.CreateObject(   strObjectId   ) 

The strObjectId parameter identifies which component to create. This is unique for each type of COM object. The values will be provided in the documentation for each object.

9.7.3. Arguments Collection

The Arguments collection contains the parameters passed to the script. This is useful for scripts that perform operations based on information specified in command-line parameters.

You can access a specific argument by specifying the parameter number as it appears on the command line. The argument count starts at 0.

The following example lists all parameters passed to the script:

 Wscript.Echo Wscript.Arguments(0) 'output the first parameter For Each arg in Wscript.Arguments 'output all parameters     Wscript.Echo arg Next 

The following example sets the file attribute on a file specified in the command line to Hidden :

 Dim objFileSystem, objFile  'check if parameter count is a single parameter.  'If not, show the command syntax If Wscript.Arguments.Count<>1 Then      Wscript.Echo " Syntax: HideFile FileName " & vbCrLf & _              " Filename: is valid path to file you wish to hide" Else     'create FileSystem object     Set objFileSystem = CreateObject("Scripting.FileSystemObject")     On Error Resume Next     'get the file specified in command line      Set objFile = objFileSystem.GetFile(Wscript.Arguments(0))     'check if error occured - file not found     If Err Then          Wscript.Echo "File:'" & Wscript.Arguments(0) & "' not found"     Else         objFile.Attributes = 2     End If End IF 

9.7.4. Quit

The Quit method forces the script to stop execution. Execution will terminate even if the script is in the middle of a loop or subroutine. Its syntax is:

 Wscript.Quit([   intExitValue   ]) 

The parameter intExitValue is the value returned to the calling program. This is useful, for example, if the script is being called from a batch file (see "if" in Chapter 6). Batch files can access the results using the ErrorLevel variable.

 scriptname.vbs If ErrorLevel 5 Goto skip 



Windows XP in a Nutshell
Windows XP in a Nutshell, Second Edition
ISBN: 0596009003
EAN: 2147483647
Year: 2003
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net