{"id":66933,"date":"2006-07-11T13:53:00","date_gmt":"2006-07-11T13:53:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/11\/how-can-i-load-list-box-options-from-a-text-file\/"},"modified":"2006-07-11T13:53:00","modified_gmt":"2006-07-11T13:53:00","slug":"how-can-i-load-list-box-options-from-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-load-list-box-options-from-a-text-file\/","title":{"rendered":"How Can I Load List Box Options From a Text File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I load list box options from a text file?<BR><BR>&#8212; MV<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"TechNet Script Center\" border=\"0\" alt=\"TechNet Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, MV. You would have to mention \u201coptions,\u201d wouldn\u2019t you? When the Scripting Guy who writes this column first joined Microsoft some five years ago, he assumed that he\u2019d 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\u2019s still writing this column mean that he was a bit \u2026 na\u00efve \u2026 when it came to how much money he\u2019d actually make over those first few years? Oh, heavens no; he continues to write this column simply because he\u2019d much rather work than fritter his time away in some tropical paradise.<\/P>\n<P>Or so he tells himself every morning; it\u2019s about the only way he can wrench himself out of bed.<\/P>\n<TABLE id=\"EZC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>True story<\/B>. When he first started here this Scripting Guy worked for a long-time Microsoft employee who <I>was<\/I> 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\u2019t know how to enjoy it?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Oh, well. For those few of us who actually <I>have<\/I> to work (which isn\u2019t necessarily the same thing as <I>wanting<\/I> to work), here\u2019s a very simple little HTA (<A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTML Application<\/B><\/A>) 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:<\/P><PRE class=\"codeSample\">&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;\n    Sub Window_OnLoad\n        ForReading = 1\n        Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\n        Set objFile = objFSO.OpenTextFile _\n            (&#8220;C:\\Scripts\\Computers.txt&#8221;, ForReading)\n        Do Until objFile.AtEndOfStream\n            strLine = objFile.ReadLine\n            Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n            objOption.Text = strLine\n            objOption.Value = strLine\n            AvailableComputers.Add(objOption)\n        Loop\n        objFile.Close\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;select size=&#8221;3&#8243; name=&#8221;AvailableComputers&#8221; style=&#8221;width:250&#8243;&gt;&lt;\/select&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>Yes, it <I>is<\/I> a fairly simple little script, isn\u2019t it? (But, then again, we only said that we had to work; we didn\u2019t say that we had to work <I>hard<\/I>.) 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):<\/P><PRE class=\"codeSample\">&lt;select size=&#8221;3&#8243; name=&#8221;AvailableComputers&#8221; style=&#8221;width:250&#8243;&gt;&lt;\/select&gt;\n<\/PRE>\n<P>As you can probably guess, this list box (christened AvailableComputers) is the list box where we\u2019ll add the options. Notice that we start off without <I>any<\/I> options assigned to AvailableComputers.<\/P>\n<P>That\u2019s all well and good, mind you, but an empty list box isn\u2019t especially exciting. So let\u2019s 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:<\/P><PRE class=\"codeSample\">ForReading = 1\nSet objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile _\n    (&#8220;C:\\Scripts\\Computers.txt&#8221;, ForReading)\n<\/PRE>\n<P>Incidentally, we\u2019re assuming that the text file consists of nothing but computer names, with one name per line. In other words, we\u2019re assuming Computers.txt looks something like this:<\/P><PRE class=\"codeSample\">atl-fs-01\natl-fs-02 \natl-fs-03 \nalt-fs-04\n<\/PRE>\n<P>This part should be pretty straightforward. We begin by defining a constant named ForReading and assigning it the value 1; we\u2019ll use this constant to tell the script that we want to open the file Computers.txt for reading. We create an instance of the <B>Scripting.FileSystemObject<\/B>, then call the <B>OpenTextFile<\/B> method in order to open our text file for reading.<\/P>\n<P>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 <B>AtEndOfStream<\/B> 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:<\/P><PRE class=\"codeSample\">strLine = objFile.ReadLine\n<\/PRE>\n<P>If you\u2019ve worked with text files before all this should be very familiar to you. (If you <I>haven\u2019t<\/I> worked with text files, then we suggest you take a look at the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_scr_iikh.mspx\" target=\"_blank\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>.) But now we\u2019re ready to do something a little more out-of-the-ordinary:<\/P><PRE class=\"codeSample\">Set objOption = Document.createElement(&#8220;OPTION&#8221;)\nobjOption.Text = strLine\nobjOption.Value = strLine\nAvailableComputers.Add(objOption)\n<\/PRE>\n<P>That\u2019s 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 <B>createElement<\/B> method to create a new, \u201cblank\u201d option object (one with the object reference objOption):<\/P><PRE class=\"codeSample\">Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n<\/PRE>\n<P>If you\u2019re familiar with HTML then you know that each option in a list box has (at least) two properties: the option <B>Text<\/B> (the label that actually appears in the list box) and the option <B>Value<\/B> (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 <B>Primary Domain Controller<\/B> in the list box, but assign that same option a Value like <B>atl-dc-01<\/B>. For this particular HTA we\u2019re 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:<\/P><PRE class=\"codeSample\">objOption.Text = strLine\nobjOption.Value = strLine\n<\/PRE>\n<P>After that all we have to do is call the <B>Add<\/B> method to add this new option to the AvailableComputers list box:<\/P><PRE class=\"codeSample\">AvailableComputers.Add(objOption)\n<\/PRE>\n<P>And that\u2019s <I>all<\/I> we have to do; from there we simply loop around and repeat the process for the remaining lines in the text file. When we\u2019re all done we should have an HTA that looks something like this:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/listbox-options.jpg\" width=\"311\" height=\"205\"> \n<P><BR>A bit on the boring side, and it doesn\u2019t actually <I>do<\/I> anything. But once you get those computer names into the list box, well, at that point the sky\u2019s the limit.<\/P>\n<P>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.. <\/P>\n<TABLE id=\"EMG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, we know: money can\u2019t buy happiness. But we\u2019d be willing to take that chance.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I load list box options from a text file?&#8212; MV Hey, MV. You would have to mention \u201coptions,\u201d wouldn\u2019t you? When the Scripting Guy who writes this column first joined Microsoft some five years ago, he assumed that he\u2019d work for two or three years, cash in all his stock [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,14,5,30],"class_list":["post-66933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I load list box options from a text file?&#8212; MV Hey, MV. You would have to mention \u201coptions,\u201d wouldn\u2019t you? When the Scripting Guy who writes this column first joined Microsoft some five years ago, he assumed that he\u2019d work for two or three years, cash in all his stock [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66933"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66933\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}