As I described in "WSH, Part 1: File Types," January 2006, InstantDoc ID 48498, Windows Script Host (WSH) is a COMbased scripting host that can execute scripts in Windows. It natively supports both JScript and VBScript, but other languages are also available if you install them separately.
WSH can execute both standalone (e.g., language-specific) scripts and Windows Script file (.wsf) scripts. The latter scripts are XML-formatted text files that can contain code in more than one scripting language. Although they're slightly more complex to write, .wsf scripts provide the script author some other useful features that are tedious or difficult to implement by using standalone scripts.
The .wsf XML File Format
The XML format that .wsf scripts use looks similar to HTML. XML uses markup tags enclosed in angle brackets (< >), called elements, that describe the data in the file. The full list of XML elements used in .wsf files is provided at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsorixmlelements.asp. A brief overview of XML syntax is as follows:
- The file should begin with the element to indicate that the file is in XML format.
- Elements are case sensitive and are enclosed in angle brackets.
- The opening tag for an element is specified as
, and the closing tag uses a forward slash after the opening angle bracket and before the element name (e.g., elementname>).
- Elements can have one or more attributes expressed in the form attribute="value". The attribute's value must be enclosed in double quotes. For example,
- If an element doesn't have a closing tag, you must use a forward slash before the closing angle bracket in the opening tag; for example, < elementname attribute="value"/>.
- Elements can include other elements as long as the tags don't overlap (i.e., the inner element must have an opening and closing tag before the outer element's closing tag).
- You can create a comment element by inserting text between the opening tag.
When you use the element, the angle brackets (< and >) and ampersand (&) characters have special meanings to the XML parser when they occur in element content and must be replaced with the character sequences <, >, and &, respectively.
Unfortunately, these characters tend to occur frequently in VBScript and JScript code. To avoid having to make the replacements while still avoiding XML parsing problems, you can enclose all VBScript and JScript code between the markers.
Listing 1 shows the minimum required XML elements for a .wsf script. There must be at least one
Note also that the referenced file should contain only code in the desired language, not XML elements. For example, you can't include code from another .wsf file.
You can include the
The type attribute is optional and indicates the argument's data type; it can be "string", "boolean", or "simple" (The default is "simple".) The type attribute affects the display of the usage message: If you use type="string", the argument will be appended with a colon (:) and the string value to indicate that a value is required. If you use type="boolean", the argument is appended with the string \[+|-\] to indicate that the user should type a plus (+) or minus (-) character after the argument name. If you use type="simple" (or omit the type attribute), the argument is displayed as is.
It's important to note that the
Listing 3 shows a sample script that uses the
Another strength of .wsf scripts is their ability to execute code in multiple languages: That is, a