Understanding Scripts and Script Execution


Scripts are simple text files that you create using Notepad or some other text editor. You can use a word processor such as WordPad to create scripts, but you must make sure that you save these files using the program’s “text only” document type.

When you first save the file, be sure to use the .vbs extension, which specifies the document as a VBScript script file. (We’re assuming you’ll be programming in VBScript, but WSH also supports other languages. If you program in a different language, you must name your files accordingly. For example, JavaScript script files must use the .js extension.)

As described in the next three sections, you have three ways to run your scripts: by launching the script files directly, by using Wscript.exe, or by using Cscript.exe.

Note

NoteWe can only scratch the scripting surface in a single chapter. Fortunately, Microsoft has an extensive set of scripting tutorials and references on its MSDN Web site:

http://msdn.microsoft.com/nhp/default.asp?contentid=28001169

Running Script Files Directly

The easiest way to run a script from within Windows XP is to launch the .vbs file directly using any of the following techniques:

  • Double-click the file in Windows Explorer.

  • Enter the file’s path and name in the Run dialog box.

  • Enter the file’s path and name at the command prompt.

Using WScript for Windows-Based Scripts

The .vbs file type has an “open” action that’s associated with WScript (Wscript.exe), which is the Windows-based front end for the Windows Script Host. In other words, launching a script file named, say, Myscript.vbs, is equivalent to entering the following command in the Run dialog box:

wscript myscript.vbs 

The WScript host also defines several parameters that you can use to control how the script executes. Note that these parameters all start with a double- slash so as to ensure the command interpreter can distinguish these parameters from any command arguments that might start with a single slash. Here’s the full syntax:

WSCRIPT filename arguments //B //D //E:engine //H:host //I //Job:xxxx //S //T:ss //X

 filename 

Specifies the name, including the path, if necessary, of the script file.

 arguments 

Specifies optional arguments required by the script. An argument is a data value that the script uses as part of its procedures or calculations.

 //B 

Runs the script in batch mode, which means script errors and Echo method output lines are suppressed (the Echo method is discussed later in this chapter).

 //D 

Enables Active Debugging. If an error occurs, the script is loaded into the Microsoft Script Debugger (if it’s installed) and the offending statement is highlighted.

 //E:engine 

Executes the script using the specified scripting engine, which is the scripting language to be used to run the script. VBScript is a popular scripting engine, and the one we use in this chapter. If you have a file that contains VBScript code but does not use the .vbs extension, you can execute the file by specifying //E:vbscript as part of the WScript command.

 //H:host 

Specifies the default scripting host. For host, use either CScript or WScript.

 //I 

Runs the script in interactive mode, which displays script errors and Echo method output lines.

 //Job:xxxx 

In a script file that contains multiple jobs, executes only the job with ID equal to xxxx.

 //S 

Saves the specified WSCRIPT arguments as the default for the current user. The following registry key is used to save these settings: HKCU\Software\Microsoft\Windows Script Host\Settings

 //T:ss 

Specifies the maximum time in seconds (ss) that the script can run before it is shut down automatically.

 //X 

Executes the entire script in the Microsoft Script Debugger (if it’s installed).

Insider Secret

A script job is a section of code that performs a specific task or set of tasks. Most script files contain a single job. However, it’s possible to create a script file with multiple jobs. To do this, first surround the code for each job with the <script> and </script> tags, and then surround those with the <job> and </job> tags. In the <job> tag, include the id attribute and set it to a unique value that identifies the job. Finally, surround all the jobs with the <package> and </package> tags. Here’s an example:

<package> <job > <script language="VBScript">     WScript.Echo "This is Job A." </script> </job> <job > <script language="VBScript"> WScript.Echo "This is Job B." </script> </job> </package>

Save the file using the .wsf (Windows Script File) extension.

For example, the following command runs Myscript.vbs in batch mode with a 60-second maximum execution time.

wscript myscript.vbs //B //T:60
Note

NoteIf you plan on writing a lot of scripts, the Microsoft Script Debugger is an excellent programming tool. If there’s a problem with a script, the debugger can help you pinpoint its location. For example, the debugger enables you to step through a script’s execution one line at a time. If you don’t have the Microsoft Script Debugger, you can download a copy from the following Microsoft site:

http://msdn.microsoft.com/scripting/

Using CScript for Command-Line Scripts

The Windows Script Host has a second host front-end application called CScript (Cscript.exe), which enables you to run scripts from the command line. At its simplest, you launch CScript and use the name of the script file (and its path, if required) as a parameter, as in this example:

cscript myscript.vbs

The Windows Script Host displays the following banner and then executes the script:

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

As with WScript, the CScript host has an extensive set of parameters you can specify:

CSCRIPT filename arguments //B //D //E:engine //H:host //I 
//Job:xxxx //S //T:ss //X //U //LOGO //NOLOGO

This syntax is identical to that of WScript, but adds the following three parameters:

//LOGO

Displays the Windows Script Host banner at startup.

//NOLOGO

Hides the Windows Script Host banner at startup.

//U

Uses Unicode for redirected input/output from the console.

Tip

It’s also possible to set some of these options by using the properties that are associated with each script file. To see these properties, right-click a script file and then select Properties. In the property sheet that appears, select the Script tab. Note that when you make changes to these properties, the Windows Script Host saves your settings in a new file that has the same name as the script file, except with the .wsh extension. (For example, if the script file is Myscript.vbs, the settings are stored in Myscript.wsh.) To use these settings when running the script, use either WScript or CScript and specify the name of the .wsh file:

wscript myscript.wsh




Insider Power Techniques for Microsoft Windows XP
Insider Power Techniques for Microsoft Windows XP (Bpg-Other)
ISBN: 0735618968
EAN: 2147483647
Year: 2005
Pages: 126

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