{"id":63613,"date":"2007-11-10T00:32:00","date_gmt":"2007-11-10T00:32:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/11\/10\/hey-scripting-guy-how-can-i-save-information-in-an-hta-as-a-tab-separated-values-file\/"},"modified":"2007-11-10T00:32:00","modified_gmt":"2007-11-10T00:32:00","slug":"hey-scripting-guy-how-can-i-save-information-in-an-hta-as-a-tab-separated-values-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-save-information-in-an-hta-as-a-tab-separated-values-file\/","title":{"rendered":"Hey, Scripting Guy! How Can I Save Information in an HTA as a Tab-Separated Values File?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! I have an HTA that includes five text boxes. How can I save the values in those text boxes to a tab-separated values file?<BR><BR>&#8212; EL<\/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=\"Script Center\" border=\"0\" alt=\"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, EL. According to their itinerary, the Scripting Guys should land in Paris sometime around 8:00 AM on Friday, November 9<SUP>th<\/SUP>. OK, technically, Paris isn\u2019t exactly Barcelona, and the Scripting Guys are <I>supposed<\/I> to be headed for Barcelona and <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/itpreview.mspx\"><B>TechEd IT Forum<\/B><\/A>. However, in order to keep travel costs to a minimum they had to stop in Paris on the way and, seeing as how Scripting Guy Jean Ross has never been to Paris, they decided to take in the sights before moving on to Barcelona.<\/P>\n<P>With that in mind, their plan is to check into their hotel, then spend the day seeing the city: Notre Dame, the Louvre, and, last but surely not least, the Eiffel Tower.<\/P>\n<TABLE id=\"EJD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. Assuming, of course, that Scripting Guy Jean Ross can fight her way past the paparazzi and the throngs of adoring fans.<\/P>\n<P>And no, Scripting Guy Greg Stemp doesn\u2019t expect to have the same problem. Or at least he didn\u2019t the first time he visited Paris.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The Scripting Guys have always had a soft spot in their hearts for the Eiffel Tower. After all, no sooner was it built than the Eiffel Tower was branded an eyesore, and angry Parisians demanded that it be torn down. According to legend, novelist Guy de Maupassant ate lunch at the Tower restaurant every day. When asked why, he replied that the restaurant was the one place in Paris he could sit without having to see the Tower.<\/P>\n<P>Today, of course, the Eiffel Tower is the symbol of Paris, and one of the most recognized and beloved structures in the world.<\/P>\n<P>So why do the Scripting Guys care about that? Well, the story of the Eiffel Tower so closely parallels the story of the Scripting Guys and the Script Center. No sooner did the Scripting Guys put up the Script Center then it was branded an eyesore, and angry Microsoft employees demanded that it be torn down. Today, of course, the Script Center is one of the most belov &#8212; well, anyway, at least it\u2019s still up, huh?<\/P>\n<P>Incidentally, he Tower itself consists of 18,038 pieces of iron held together with well over two million rivets. That\u2019s an impressive piece of work. But, to be honest, it\u2019s nowhere near as impressive as a script that can save HTA text box values to a tab-separated values file:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Save as TSV File&lt;\/title&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;\n    Sub SaveData\n        Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\n        If objFSO.FileExists(&#8220;C:\\Scripts\\Test.tsv&#8221;) Then\n           Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.tsv&#8221;, 8)\n            strLine = UserName.Value &amp; vbTab &amp; Email.Value &amp; vbTab &amp; _\n                Title.Value &amp; vbTab &amp; Country.Value &amp; vbTab &amp; Product.Value\n            objFile.WriteLine strLine\n            objFile.Close\n        Else\n            Set objFile = objFSO.CreateTextFile(&#8220;C:\\Scripts\\Test.tsv&#8221;)\n            strLine = UserName.Value &amp; vbTab &amp; Email.Value &amp; vbTab &amp; _\n                Title.Value &amp; vbTab &amp; Country.Value &amp; vbTab &amp; Product.Value\n            objFile.WriteLine strLine\n            objFile.Close\n        End If\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;UserName&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;Email&#8221; size=&#8221;25&#8243; &gt;&lt;p&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;Title&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;Country&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;Product&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;\n    &lt;input type=&#8221;button&#8221; value=&#8221;Run Button&#8221; onClick=&#8221;SaveData&#8221;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>This is actually a much simpler script than it might first appear. To begin with the &lt;BODY&gt; of the HTA consists of 5 text boxes and a button. All the text boxes have a unique name (like UserName) and just for the heck of it, each text box has been given a <B>size<\/B> of 25 characters. (Which simply determines the width of the box, and has nothing to do with the number of characters than can be typed in the text box.) For example, here\u2019s the tag for the UserName text box:<\/P><PRE class=\"codeSample\">&lt;input type=&#8221;text&#8221; name=&#8221;UserName&#8221; size=&#8221;25&#8243;&gt;&lt;p&gt;\n<\/PRE>\n<P>Our button is equally simple: it has the <B>value<\/B> (label) <I>Run Button<\/I>, and is configured to respond to the <B>onClick<\/B> event:<\/P><PRE class=\"codeSample\">&lt;input type=&#8221;button&#8221; value=&#8221;Run Button&#8221; onClick=&#8221;SaveData&#8221;&gt;\n<\/PRE>\n<P>As you can see, each time the button is clicked, the SaveData subroutine will be executed.<\/P>\n<P>Coincidentally enough, we were just going to talk about the SaveData subroutine. The first thing we do inside this subroutine is create an instance of the <B>Scripting.FileSystemObject<\/B>; that\u2019s the object we need to use in order to write to a text file. Once we have this object in hand, we then use the <B>FileExists<\/B> method to determine whether or not the file C:\\Scrips\\Test.tsv already exists:<\/P><PRE class=\"codeSample\">If objFSO.FileExists(&#8220;C:\\Scripts\\Test.tsv&#8221;) Then\n<\/PRE>\n<P>Why do we bother with that? Well, if the file exists then we want to simply append new data to the end of the file; if the file <I>doesn\u2019t<\/I> exist then we need to first create the file and then add our data to it. The FileSystemObject, as many of you know, is a fickle little thing: if you aren\u2019t careful about how you use the FileSystemObject to open a text file you might get very unexpected results. As system administrators, we don\u2019t like very unexpected results. Thus we first check to see whether or not Test.tsv exists, then take the appropriate course of action.<\/P>\n<P>Let\u2019s assume that Test.tsv <I>does<\/I> exist. In that case, we use this line of code to open the file for appending (that\u2019s what the second parameter, the <B>8<\/B>, is for):<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.tsv&#8221;, 8)\n<\/PRE>\n<P>Once the file is open we use this line of code to construct the line to be appended to the file:<\/P><PRE class=\"codeSample\">strLine = UserName.Value &amp; vbTab &amp; Email.Value &amp; vbTab &amp; _\n    Title.Value &amp; vbTab &amp; Country.Value &amp; vbTab &amp; Product.Value\n<\/PRE>\n<P>All we\u2019re doing here is stringing together the <B>Values<\/B> of our five text boxes, taking care to separate each value by a tab (that\u2019s what the VBScript constant <B>vbTab<\/B> is for). There\u2019s nothing fancy here at all: we\u2019re taking the Value of the UserName textbox and then adding a tab onto the end of that value; then adding the value of he Email textbox and adding a tab onto the end of <I>that<\/I>; then \u2013 well, you can probably figure out the rest for yourself. When we\u2019re all done, that means the variable strLine will have a value similar to this:<\/P><PRE class=\"codeSample\">Kenmyer     kmyer@fabrikam.cim     Manager     US     X492\n<\/PRE>\n<P>From there we call the <B>WriteLine<\/B> method to append this value to the text file, then call the <B>Close<\/B> method to close that text file.<\/P>\n<P>The process is similar if the file Test.tsv <I>doesn\u2019t<\/I> exist. The only real difference is that, in that case, we don\u2019t open the file for appending. (Why not? Because the file doesn\u2019t exist.) Instead, we call the <B>CreateTextFile<\/B> method and create the file Test.tsv:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.CreateTextFile(&#8220;C:\\Scripts\\Test.tsv&#8221;)\n<\/PRE>\n<P>Once we\u2019ve done that we can construct the line to be added to the file, use WriteLine to write that line to the file, and then use the Close method to close the file. Game, set, and match.<\/P>\n<P>As for the Scripting Guys, well, while you\u2019re sitting in your office, drinking cold coffee and pretending to work, with any luck at all the Scripting Guys are eating crepes and headed towards the Louvre. What\u2019s that? Really? You say that the <I>Mona Lisa<\/I> is at the Louvre? Well, whatta you know? To be honest, we weren\u2019t planning on staying that long; we figured we\u2019d only stay long enough to drop off today\u2019s column, give the curators permission to display the thing, and then head for Notre Dame. But maybe we\u2019ll have to see what else is at the Louvre besides Scripting Guys scripts. Might be fun.<\/P>\n<P>And after that? Then it\u2019s on to Barcelona!<\/P>\n<P>Well, after we go to the Eiffel Tower, that is. And Versailles, we have to go out to Versailles. And after we eat a few more crepes, maybe a <I>lot<\/I> more crepes. But, sooner or later, it\u2019s on to Barcelona. See you all on Monday!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have an HTA that includes five text boxes. How can I save the values in those text boxes to a tab-separated values file?&#8212; EL Hey, EL. According to their itinerary, the Scripting Guys should land in Paris sometime around 8:00 AM on Friday, November 9th. OK, technically, Paris isn\u2019t exactly Barcelona, [&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,5,30],"class_list":["post-63613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have an HTA that includes five text boxes. How can I save the values in those text boxes to a tab-separated values file?&#8212; EL Hey, EL. According to their itinerary, the Scripting Guys should land in Paris sometime around 8:00 AM on Friday, November 9th. OK, technically, Paris isn\u2019t exactly Barcelona, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63613","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=63613"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63613\/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=63613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}