Subscribe to Windows IT Pro
March 13, 2002 12:00 AM

Scripting Solutions with WSH and COM: Add Line Numbers to Improve Script Debugging

Windows IT Pro
InstantDoc ID #24372
Rating: (0)
Downloads
24372.zip

Writing the New File
Now that the strInput variable holds the name of the .vbs file to which I want to add line numbers, I need to open this file and read the contents line by line. To do so, I use the FileSystemObject::OpenTextFile method with the fso variable I created during initialization (the code at callout B in Listing 1). To ensure that the script opens the file in read-only mode, I defined during initialization a constant called ForReading, which I use as the second parameter to the OpenTextFile method. Then, I attach the open file to the filSource variable. Now I need to open a new temporary file that I can write to.

During initialization, I defined the TEMP_FILE constant, whose value is the temporary file's pathname, and the ForWriting constant, whose value lets you write to a file. To open the temporary file, I use the FileSystemObject::OpenTextFile method again—this time using the new filename, the ForWriting constant, and a value of True, which indicates that the file should be overwritten if it already exists.

Because the script must go through the entire .vbs file line by line, I use a While...Wend statement. The script checks the TextStream::AtEndOfStream property, which returns a value of True or False depending on whether the script is at the end of the file.

To actually write a line to the destination file, I use the TextStream::WriteLine method. The script writes to the file data that I pass to this method. The data consists of the line number (which I used the TextStream::Line property to retrieve), a string consisting of a colon (:) and a space, and the existing line read from the .vbs file. The result is a new line in the temporary file for every line in the .vbs file, but this time with the line number and a colon prefixing each line. Finally, I use the TextStream::Close method to close both files.

Bringing It All Together
So far, I've created a new file in the TEMP_FILE location without altering the original .vbs file. The last step is to open the temporary file on screen. The simplest way to open this file is to place the text file as a parameter to the WScript::Shell Run method (the code at callout C in Listing 1). This method forces the OS to use the default viewer to open the file. In this case, TEMP_FILE has a .txt extension, so the default viewer is Notepad. However, you could give TEMP_FILE a .lin extension, then use the code

wshShell.Run "notepad " & _TEMP_FILE

to force the file to open in Notepad or any other viewer.

The last two lines of code in ShowLineNumbers.vbs might seem a bit strange. When Notepad opens the text file, the application places the file in memory, which means that you can delete the underlying file without causing Notepad to crash or experience problems. With these two lines of code, I'm forcing the script to wait 10 seconds after Notepad opens the file with the line numbers, then to delete the underlying file.

Next Time
Now that you know how to enumerate the line numbers in .vbs files, troubleshooting the scripts you use text editors to create will be much easier. In my next article, I'll follow up on "Scripting Solutions with WSH and COM: Scheduling Automated Tasks," March 2002, InstantDoc ID 23935, and show you how to have the WSH CScript engine call a VBScript file.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.