2008 Winter Scripting Games

Solution to Beginner VBScript Event 4: Count Yourself In

Event 4 Solution


VBScript solution to Event 4 in the 2008 Winter Scripting Games.

Solutions are also available for Windows PowerShell and Perl.

*

Event 4 – Count Yourself In

To correctly solve this event you needed to be able to determine the name of the running script and read from that script file. Here’s one possible solution:

Const ForReading = 1

strScriptName = Wscript.ScriptFullName

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strScriptName, ForReading)

strScriptContents = objFile.ReadAll

strScriptContents = Replace(strScriptContents, vbCrLf, "")
Wscript.Echo Len(strScriptContents)

objFile.Close

The solution to this event could be considered pretty easy just because there was no need for loops or conditional statements or anything like that; it’s a pretty straightforward script.

We start by defining a constant, ForReading, which we’ll use to read from the script file. The next line is probably the key to this script:

strScriptName = Wscript.ScriptFullName

If you got this line you were well on your way to solving this event. (And if you read the Scripting Games Tips you got this line.) All we’re doing here is reading the Wscript.ScriptFullName property. This property contains the full path and filename to the running script – this script, as a matter of fact. We place that name in the variable strScriptName.

Now we want to open this script file. Is it a problem to open a file while the script is running? Not at all. The script is running in the computer’s memory, and opening the file simply opens a second copy in another section of memory; the two copies really don’t have much to do with each other at that stage. In order to open the file, we need to create a FileSystemObject object, then call OpenTextFile on that object:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strScriptName, ForReading)

Notice when we call OpenTextFile we pass two parameters: the name of the file we’re opening (in this case the name of the script, stored in strScriptName); and the constant ForReading, which says we’re opening the file to read from it, not write to it. (The FileSystemObject doesn’t let you do both at the same time, and we don’t need to do any writing for this script anyway.)

To get at the contents of our open file, we next call the ReadAll method:

strScriptContents = objFile.ReadAll

This method reads the entire contents of the file, and we assigned those contents to the variable strScriptContents.

We could simply count the characters in the script from here, but if you did that you didn’t get any points for this event. The instructions for this event said that the count should not include the carriage-return linefeed. That means that before we count the characters in the file we need to strip out all the carriage-return linefeed characters. We do that with the Replace function:

strScriptContents = Replace(strScriptContents, vbCrLf, "")

We’ve passed three parameters to the Replace function:

strScriptContents The string we want to do a search-and-replace on. In this case, that’s the string that contains the contents of the script file.

vbCrLf The character (or string) we want to replace. Here we’re using the VBScript constant vbCrLf, which represents the carriage-return linefeed character.

"" The character (or string) we’re replacing the carriage-return linefeed with. Because we want to simply strip out all the carriage-return linefeed characters, we’re replacing them with an empty string, or nothing.

All that’s left to do is count the number of characters that are left in the string and display that number. We do that with this line:

Wscript.Echo Len(strScriptContents)

The Len function takes a string as a parameter and outputs the length of – the number of characters in – that string. We call Wscript.Echo on that return value. Okay, we sort of took a shortcut here. This might be a little clearer:

iLength = Len(strScriptContents)
Wscript.Echo iLength

Is that better? We retrieve the length of the string and assign it to the variable iLength. Then we call Wscript.Echo to display that number. The original line of code was just a shorthand way of doing that.

All that’s left is the close the file and we’re done:

objFile.Close

Top of pageTop of page