Section 9.3. Executing Scripts

9.3. Executing Scripts

Windows XP supplies two programs used to run WSH scripts (whatever language they are written in). wscript.exe is used to run WSH scripts from the Windows graphical environment, and cscript.exe is used to run them from the command line or from within batch files. Both programs are fully documented in Chapter 4.

9.3.1. Using wscript.exe

Using wscript.exe , you can run scripts under Windows in the following ways:

  • Double-click on script files or icons listed in My Computer, Windows Explorer, the Find window, the Start menu, or on the Desktop.

  • If you double-click a script file whose extension has not yet been associated with wscript.exe , an Open With dialog box appears asking which program you would like to use to open the file. After choosing WScript, check the "Always use this program to open this file" checkbox, and WScript is registered as the default application for all files with the same extension as the one you double-clicked.

  • Enter wscript , followed by a script name at the Run command on the Start menu or at any command prompt. You must enter the full name of the script, including the file extension and any necessary path information.

Although the wscript.exe application has several command-line parameters (see "Windows Script Host" in Chapter 4), it's more convenient to use the Properties window of a script file (right-click the file and select Properties). Options are set in the usual way, but the properties are not saved in the script file or in the Registry as you might expect; instead, a new file with the .wsh extension is created (the filename prefix is the same as the original script file). The file is a plain text file with the familiar .ini format, and can be edited with a text editor such as Notepad.

Figure 9-1 shows the Script tab of the properties for a script file. You can choose whether the script should time out, what the timeout for the script should be, and whether the script logo should be displayed when the script is run.

Figure 9-1. The Script tab in a .wsh property sheet

A sample .wsh file might look like this:

 [ScriptFile] Path=C:\Scripts\MyScript.vbs [Options] Timeout=30 DisplayLogo=1 BatchMode=0 

The Path setting in the [ ScriptFile ] section identifies the script file that the .wsh file is associated with. The keys in the [ Options ] section correspond to settings on the Script tab.

A .wsh file can be treated as if it were a Windows Shortcut; you can run a .wsh file directly by double-clicking on it, or by using it as the script name parameter with wscript.exe or cscript.exe . If you delete the .wsh file, the original script will be unaffected. The .wsh files are very useful for administration purposes. Since you can run a .wsh file directly (rather than invoking the script itself), you can use a text editor to create multiple .wsh files for the same script, each containing different parameters but the same script name.

9.3.2. Using cscript.exe

Using cscript.exe , you can run command-line based WSH scripts from the command prompt. In most circumstances, you'll want to use the Windows-based wscript.exe . However, cscript.exe is more appropriate when used with command prompt applications or when you need the script to use STDOUT (standard output), such as when writing CGI scripts for web servers. This is illustrated by the Wscript.Echo method described in Section 9.6 later in this chapter.

Like wscript.exe , cscript.exe has several command-line parameters documented in "Windows Script Host" in Chapter 4.



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