When you use text editors such as Notepad to create long scripts, receiving error messages such as Error in line 162 from Windows Script Host (WSH) as it interprets the lines of code can be annoying. The messages are annoying not because the script has errors, but because you don't know which line is line 162. Now, I could have told you to buy SAPIEN Technologies' Primalscript or use Microsoft Script Debugger (which you can find in the Microsoft Windows 2000 Server Resource Kit or download from http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdn-files/027/001/731/msdncompositedoc.xml); instead, I decided to write a script to enumerate the line numbers in .vbs files.
Laying the Groundwork
I wanted to create a script that would add line numbers to the lines of code in a .vbs file. However, I didn't want to append the numbers to the code in the original .vbs file because the line numbers would cause the code to fail. So, I needed to create a temporary file and copy the .vbs file's code into it, appending line numbers to each line of code in the temporary file during the copy. To make the script easy to use, I wanted to use the WSH drag-and-drop technology that I showed you in "Scripting Solutions with WSH and COM: Using WSH Drag and Drop to Send Faxes in Win2K," December 2001, InstantDoc ID 23041. Using this WSH technology, I created a script called ShowLineNumbers.vbs onto which I can drag other scripts to generate and open a temporary file with the contents appended by line numbers. ShowLineNumbers.vbs, which Listing 1, page 14, shows, has four distinct sections:
- InitializationI set up the constants and variables.
- ArgumentsI make sure the user is passing only one argument (i.e., the pathname to one .vbs file) to the script (the code at callout A in Listing 1).
- New file creationI create the temporary file that includes line numbers (the code at callout B in Listing 1).
- EndI view, then delete the temporary file (the code at callout C in Listing 1).
You can download ShowLineNumbers.vbs from the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com). Let's go through this simple script.
Script Arguments
In the code at callout A, I use the value returned by the WScript::Arguments collection's Count property to make sure the user has dragged one .vbs file (i.e., passed in one argument). If this property's value is greater than 1, the script ignores all the passed-in arguments and displays an input box that tells the user only one file is allowed at a time and prompts the user to enter one pathname. I then use VBScript's Trim function to remove the leading and trailing spaces from the entered pathname before placing the name into the strInput variable. If the Count property's value is 0, the script hasn't received a drag-and-drop file. In other words, the user manually executed the script (e.g., by double-clicking it). In this case, the script displays an input box that prompts the user for a pathname. I again use the Trim function to remove the leading and trailing spaces from the entered pathname before placing the name into the strInput variable. If the Count property's value isn't greater than 1 and isn't 0 (i.e., the value is 1), I place the name of the drag-and-drop file into strInput.
Next, I verify that strInput actually contains a value. For example, the user could just have pressed Enter or clicked OK in the input box, which means that the pathname is blank. If such is the case, the script outputs an error message stating that the user hasn't specified a file and quits. If no error has occurred, I verify that a file with the specified name exists. To do so, I use the FileSystemObject::FileExists method to which I connected the fso variable during the initialization section of the script, then see what Boolean value the method returns. If the method returns the value of True, strInput contains a full, valid pathname and the script continues. If the method returns the value of False, the script issues another error message stating that the file doesn't exist and quits.
This pathname-retrieval process is quite cumbersome. Fortunately, you don't have to go through all this rigmarole. If you're sure that you'll always drag only one file onto ShowLineNumbers.vbs and that any mistakes you make will generate errors, you can simply replace the code at callout A with the code that Listing 2 shows. This new code states that if the number of arguments passed in to the script is anything other than 1, ShowLineNumbers.vbs quits. Otherwise, strInput is set to the value of the argument. When you drag this argument onto ShowLineNumbers.vbs, you know that the pathname is valid because the OS passed it in as part of the WSH drag-and-drop operation.