The WriteIt Function
Purpose: Outputs error messages or other strings to a text file
WriteIt is a very simple function that I use all the time to output error messages to a log file. I usually accumulate the errors into a string, which I then output to a log file at the end of the script. When I want to write error messages more frequently, I use this function to append them to a file.
Listing 1 shows the code for the WriteIt function. In this function, the aStr variable contains the string of accumulated errors and the strOP_F variable contains the log file's pathname. At the end of the function, I set the objects I used to Nothing. Although it's not necessary to set objects to Nothing in VBScript, I do it as a matter of good practice, as you usually have to do in other languages.
To call the WriteIt function in a script, you use the syntax
WriteIt Text, LogPath
where Text is the text you want to write and LogPath is the pathname of the text file in which you want to write that text. These two mandatory arguments can be variables (e.g., aStr, strOP_F) or strings (e.g., "Hello World", "C:\Test.txt").
Share Your Snippets
Do you have some favorite snippets you’d like to share with other Windows Scripting Solutions readers? Email your snippet and a description (500 words or less) of what it does and how to use it in a script to [email protected]. Please include your full name and phone number. We edit submissions for style, grammar, and length. If we print your submission, you’ll get $100.