Copying VBScript Files into WSF Files
A better approach for converting VBScript or JScript files to .wsf files is to copy the VBScript or JScript file into the new WSF script. In this case, you'll get a larger file, but you don't need to worry about inner dependencies that can make usage problematic. The ScriptToWsfCopy script, which Listing 2 shows, performs this copy operation.
The ScriptToWsfCopy script works in much the same way as the ScriptToWsf script. ScriptToWsfCopy creates a new text file with a .wsf extension and starts filling it with XML code. Then, the script opens the <script> tag and assigns the proper language attribute. The next step is where the two routines differ. Instead of defining the src attribute, ScriptToWsfCopy simply reads all the original file's contents and writes it to the output stream.
The VBScript or JScript code is now part of the XML bodywhich might be cause for further problems. For example, what happens if the VBScript or JScript code includes the greater-than operator (>)? Such a character has a special meaning to XML parsers and must be escaped so that the XML parser treats it as any other character. Therefore, two small changes to the final .wsf file are necessary. First, you must put the file into strict XML mode by declaring the following directive at the top of the file:
<?XML version="1.0" ?>
In this mode, the XML parser will recognize the following CDATA markers, which comprise the second change:
<![CDATA\[
escaped code
]]>
All the text (in this case, code) wrapped by <![CDATA\[ and ]]> delimiters is considered raw text and isn't processed.
From the user's standpoint, the effect of using ScriptToWsf and ScriptToWsfCopy is exactly the same. However, in the latter case, you have one file to take care of, so maintenance and management are a bit easier.
Configuring the Windows Shell
You can use both ScriptToWsf and ScriptToWsfCopy as drop targets on the desktop or in any Windows folders. However, the Windows shell, in collaboration with the system registry, provides an even better method to expose both scripts. By tweaking the registry, you can add new items to the context menu of a VBScript file. These items would point to the scripts and work on the VBScript file that you selected. Listing 3 shows a short script that you need to run so that you can properly configure the Windows shell.
To add a context menu item to VBScript files, you must create a subkey under the HKEY_CLASSES_ROOT\VBSFile\Shell registry subkey. The name of this subkey is arbitrary; I've chosen ConvertToWsf. The system will use this name as the menu item text unless you enter descriptive text in the key's default value. The ConvertToWsf subkey must also have a subkey called Command, the default value of which points to the command to execute. To associate this subkey value with the ScriptToWsfCopy.vbs script, enter the following command:
wscript.exe "C:\scripttowsfcopy.vbs" "%L"
The script must reside in a public folder, or you must specify its full path. (Modify this line accordingly for your environment.) The %L placeholder represents the fully qualified name of the VBScript file that you selected in the Windows Explorer view.
After the shell extension is in place, right-click a VBScript file and choose the new Convert to WSF option. The system will instantly create the new file in the same folder. You've now converted your old VBScript file to a more modern .wsf file.
The script in Listing 3 works for VBScript files, but you can easily adapt it to work with any other file extension. Just replace the line
classType = ".vbs"
with a line that points to a different file extension, such as
classType = ".js"
Note that the file extension points you to the class indexer node for the type. This node is simply the name of the HKEY_CLASSES_ROOT key under which the system has stored any relevant information about the class type. This class type is VBSFile for .vbs files and JSFile for .js files. To cancel the changes in the registry for VBScript files, you need only remove the HKEY_CLASSES_ROOT\VBSFile\Shell\ConvertToWsf subkey.
Only the First Step
I've often used this article's small but useful scripts to simplify migrations from old-fashioned VBScript files to .wsf files. However, such tools are only the first step toward fully understanding the potential of .wsf files. Now that you have an XML schema successfully running your old script, you're ready for the next step: exploiting other aspects of the WSF schema.