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. . . .