How Can I Load List Box Options From a Text File?

How Can I Load List Box Options From a Text File?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I load list box options from a text file?

-- MV

SpacerHey, Scripting Guy! AnswerTechNet Script Center

Hey, MV. You would have to mention “options,” wouldn’t you? When the Scripting Guy who writes this column first joined Microsoft some five years ago, he assumed that he’d work for two or three years, cash in all his stock options, and then spend the rest of his life lazing away on the island of Tahiti. So does the fact that he’s still writing this column mean that he was a bit … naïve … when it came to how much money he’d actually make over those first few years? Oh, heavens no; he continues to write this column simply because he’d much rather work than fritter his time away in some tropical paradise.

Or so he tells himself every morning; it’s about the only way he can wrench himself out of bed.

True story. When he first started here this Scripting Guy worked for a long-time Microsoft employee who was rich: she paid cash for a $1 million house on Lake Washington. Despite that, she came to work every day; she even worked late nights and on weekends. Forget all that stuff about youth being wasted on the young; what about wealth being wasted on people who don’t know how to enjoy it?

Oh, well. For those few of us who actually have to work (which isn’t necessarily the same thing as wanting to work), here’s a very simple little HTA (HTML Application) that, on startup, reads computer names from a text file and then configures each of those computer names as an option in a list box:

<SCRIPT Language="VBScript">
    Sub Window_OnLoad
        ForReading = 1
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.OpenTextFile _
            ("C:\Scripts\Computers.txt", ForReading)
        Do Until objFile.AtEndOfStream
            strLine = objFile.ReadLine
            Set objOption = Document.createElement("OPTION")
            objOption.Text = strLine
            objOption.Value = strLine
            AvailableComputers.Add(objOption)
        Loop
        objFile.Close
    End Sub
</SCRIPT>

<body>
    <select size="3" name="AvailableComputers" style="width:250"></select>
</body>

Yes, it is a fairly simple little script, isn’t it? (But, then again, we only said that we had to work; we didn’t say that we had to work hard.) The script consists of two parts: a subroutine (named Window_OnLoad) that runs any time the HTA is started or refreshed, and a simple little list box that displays three options at a time (and has a width of 250 pixels):

<select size="3" name="AvailableComputers" style="width:250"></select>

As you can probably guess, this list box (christened AvailableComputers) is the list box where we’ll add the options. Notice that we start off without any options assigned to AvailableComputers.

That’s all well and good, mind you, but an empty list box isn’t especially exciting. So let’s see what we can do about that. Inside the Window_OnLoad subroutine we start off with code designed to open the text file C:\Scripts\Computers.txt:

ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile _
    ("C:\Scripts\Computers.txt", ForReading)

Incidentally, we’re assuming that the text file consists of nothing but computer names, with one name per line. In other words, we’re assuming Computers.txt looks something like this:

atl-fs-01
atl-fs-02 
atl-fs-03 
alt-fs-04

This part should be pretty straightforward. We begin by defining a constant named ForReading and assigning it the value 1; we’ll use this constant to tell the script that we want to open the file Computers.txt for reading. We create an instance of the Scripting.FileSystemObject, then call the OpenTextFile method in order to open our text file for reading.

With the file open we set up a Do Until loop that runs until we reach the end of the file (or, more technically, until the AtEndOfStream property is True). We then use this line of code to read the first line in the text file and store the value in a variable named strLine:

strLine = objFile.ReadLine

If you’ve worked with text files before all this should be very familiar to you. (If you haven’t worked with text files, then we suggest you take a look at the Microsoft Windows 2000 Scripting Guide.) But now we’re ready to do something a little more out-of-the-ordinary:

Set objOption = Document.createElement("OPTION")
objOption.Text = strLine
objOption.Value = strLine
AvailableComputers.Add(objOption)

That’s right: these four lines of code add the name of the first computer (i.e., the first line in the text file) to the AvailableComputers list box. To do that we start off by using the createElement method to create a new, “blank” option object (one with the object reference objOption):

Set objOption = Document.createElement("OPTION")

If you’re familiar with HTML then you know that each option in a list box has (at least) two properties: the option Text (the label that actually appears in the list box) and the option Value (the value assigned to that particular option). The Text and Value properties do not have to be the same; for example, you could display a label like Primary Domain Controller in the list box, but assign that same option a Value like atl-dc-01. For this particular HTA we’re fine with the Text and Value being the same; consequently, we use these two lines of code to assign the value of strLine to both of these properties:

objOption.Text = strLine
objOption.Value = strLine

After that all we have to do is call the Add method to add this new option to the AvailableComputers list box:

AvailableComputers.Add(objOption)

And that’s all we have to do; from there we simply loop around and repeat the process for the remaining lines in the text file. When we’re all done we should have an HTA that looks something like this:

Hey, Scripting Guy!


A bit on the boring side, and it doesn’t actually do anything. But once you get those computer names into the list box, well, at that point the sky’s the limit.

Or at least it is for those of you who have the time to play around with things like HTAs. As for the rest of us, well, back to the salt mines..

Note. Yes, we know: money can’t buy happiness. But we’d be willing to take that chance.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • So how do I make these options run a script?