One of the most common tasks that I need to perform in Windows Script Host (WSH) scripts is to determine the script host (i.e., cscript.exe or wscript.exe) that's executing the current script. Determining the script host is important when, for example, a script uses the WScript object's Echo method repeatedly to display output. You don't want to use wscript.exe for such a script because if you did, the script would display a separate dialog box for each echoed string. Also, some scripts need access to certain properties of the WScript object that don't exist when the script host is wscript.exe (e.g., the StdIn, StdOut, and StdErr properties).
To allow my scripts to determine the script host, I wrote the ScriptHost function. Callout A in Listing 1 shows the VBScript version of this function. Callout A in Listing 2 shows the JScript version.
Both versions of the ScriptHost function use the WScript object's FullName property, which returns the fully qualified path and filename of the script host. The VBScript and JScript functions then use language-appropriate methods for extracting the filename and lowercasing it.
The VBScript version uses the Mid function to extract the filename. This function returns a substring from a string and has three arguments. The first argument specifies the string from which you want to extract the substring. In this case, it's the string returned by the FullName property. The second argument specifies the point at which to start extracting the substring. In this case, the Mid function starts two characters past the number of characters in the string returned by the WScript object's Path property. The Path property's string specifies the directory containing the script host. You need to start two characters past the Path property's length for two reasons. First, the Mid function counts characters starting with the value of 1 rather than 0. Second, the Path property doesn't return the backslash character (\) that separates the path from the filename. The third argument specifies the number of characters to return. Including this argument is optional. If you omit it, the Mid function returns the remainder of the string. In this case, the argument is omitted, so the remainder of the substring is returned. The ScriptHost function then uses VBScript's LCase function to lowercase that substring.
The JScript version of ScriptHost function uses the same methodology as the VBScript version. The JScript version uses the substr method to return the path portion of the string returned by the WScript object's FullName property. JScript strings are 0-based rather than 1-based, so the ScriptHost function needs to count only one character past the number of characters returned by the WScript object's Path property to account for the backslash character. The function then uses the toLowerCase method to lowercase the resulting substring.
Listing 3 and Listing 4 demonstrate how you can use the ScriptHost function in VBScript and JScript code, respectively, to abort a script if it's not being run by cscript.exe. In both listings, the code uses the WScript object's Quit method to abort the script.