{"id":69983,"date":"2005-04-20T18:47:00","date_gmt":"2005-04-20T18:47:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/20\/how-can-i-pass-command-line-variables-to-an-hta-when-it-starts\/"},"modified":"2005-04-20T18:47:00","modified_gmt":"2005-04-20T18:47:00","slug":"how-can-i-pass-command-line-variables-to-an-hta-when-it-starts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-pass-command-line-variables-to-an-hta-when-it-starts\/","title":{"rendered":"How Can I Pass Command-Line Variables to an HTA When It Starts?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I pass command-line variables to an HTA when it starts?<BR><BR>&#8212; DM<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, DM. Ok, we admit it: you almost had us on this one. (Not that being able to stump the Scripting Guys is particularly hard, mind you.) We had no idea whether you could pass command-line variables to an HTA, and even less idea how we could capture those command-line variables. But then we stumbled across the <B>commandLine<\/B> property and, after a bit of hacking around, came up with a solution.<\/P>\n<TABLE class=\"dataTable\" id=\"E3C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. If you aren\u2019t familiar with the acronym HTA, HTA is short for HTML Application. An HTML Application provides a way for you to wrap up your scripts in a graphical user interface, complete with radio buttons, check boxes, dropdown lists and what-have-you. For more information about HTAs, check out our new <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The commandLine property simply reports back the command used to start an HTA. Because HTAs are GUI tools, they are typically started simply by double-clicking the file icon in Windows Explorer or My Computer. Thus the commandLine is nothing more than the path to the HTA file, something like this (note that the double quotes are part of the value):<\/P><PRE class=\"codeSample\">&#8220;C:\\Scripts\\Cmdline.hta&#8221;\n<\/PRE>\n<P>Now suppose you type something like this at the command prompt:<\/P><PRE class=\"codeSample\">c:\\scripts\\cmdline.hta arg1 arg2\n<\/PRE>\n<P>Guess what the commandLine property returns <I>then<\/I>? That\u2019s right; the entire command string, with the path surrounded by double quotes and the two arguments tacked onto the end:<\/P><PRE class=\"codeSample\">&#8220;c:\\scripts\\cmdline.hta&#8221; arg1 arg2\n<\/PRE>\n<P>In other words, we can capture the command-line arguments; as you can see, both <I>arg1<\/I> and <I>arg2<\/I> are in the value returned by the commandLine property. All we have to do now is figure out how to tease those values out of there.<\/P>\n<P>Our first thought was to remove the double quotes and then use the VBScript <B>Split<\/B> function to carve the command-line into an array. Our intention was to split the string on each blank space, which would give us an array consisting of the following elements:<\/P><PRE class=\"codeSample\">c:\\scripts\\cmdline.hta\narg1\narg2\n<\/PRE>\n<P>That worked, but there was a problem. Suppose you had a blank space in the HTA path; for example, suppose our path was <B>C:\\Scripts\\My HTAs\\Cmdline.hta<\/B>. That \u201cextra\u201d blank space was a problem; if we split the string on each blank space we\u2019d get an array that looked like this:<\/P><PRE class=\"codeSample\">c:\\scripts\\my\nhtas\\cmdline.hta\narg1\narg2\n<\/PRE>\n<P>Not so good. Therefore, to work around that problem we invented a new rule: we had to surround our command-line arguments with double quote marks. In other words, to start our HTA we had to type something like this from the command prompt:<\/P><PRE class=\"codeSample\">c:\\scripts\\cmdline.hta &#8220;arg1&#8221; &#8220;arg2&#8221;\n<\/PRE>\n<P>Why? Well, this returns a commandLine that looks like this:<\/P><PRE class=\"codeSample\">&#8220;c:\\scripts\\cmdline.hta&#8221;  &#8220;arg1&#8221; &#8220;arg2&#8221;\n<\/PRE>\n<P>Each argument is now enclosed within double quotes, just like the path is. If we now split on the double quote mark (<B>chr(34)<\/B>) we\u2019ll get an array that looks like this:<\/P><PRE class=\"codeSample\">&lt;blank line&gt;\nc:\\scripts\\cmdline.hta\n&lt;blank space&gt;\narg1\n&lt;blank space&gt;\narg2\n&lt;blank line&gt;\n<\/PRE>\n<P>In other words, every other line in the array has data of interest to us. Why do the other lines consist of &#8211; for all intents and purposes &#8211; nothing at all? Well, to mimic the effect replace each <B>&#8220;<\/B> in this sentence with a carriage return-linefeed and see what you get:<\/P><PRE class=\"codeSample\">&#8220;c:\\scripts\\cmdline.hta&#8221;  &#8220;arg1&#8221; &#8220;arg2&#8221;\n<\/PRE>\n<P>That\u2019s right:<\/P><PRE class=\"codeSample\">c:\\scripts\\cmdline.hta<\/p>\n<p>arg1<\/p>\n<p>arg2\n<\/PRE>\n<P>Believe it or not, that\u2019s OK. Because we have an array, we can use a For Next loop to cycle through all the items. Furthermore, we can use the <B>Step<\/B> parameter to loop through every other line. For example, suppose we have the following array:<\/P><PRE class=\"codeSample\">A\nB\nC\nD\n<\/PRE>\n<P>We can start with the second element (or item 1, because the first element in an array is item 0) and set the Step value to 2. That will read the second element (B) and then skip to the fourth element (D). And that\u2019s exactly what we\u2019ll do in our HTA. We\u2019re going to start our For Next loop with item 3. Why item 3? Well, item 3 is the fourth element in the array. We don\u2019t care about any of the first three elements: they consist of a blank line, the path to the HTA file, and a blank space. Element 4 is our first argument, so we\u2019ll start there, then skip to item 6. (Remember, item 5 will just be a blank space.) <I>That\u2019s<\/I> how we\u2019ll extract just the command-line arguments for the HTA.<\/P>\n<P>Here\u2019s the complete HTA code (remember to save this as a .HTA file and not a .VBS file):<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Command Line Agruments&lt;\/title&gt;<\/p>\n<p>&lt;HTA:APPLICATION \n     ID=&#8221;objTestHTA&#8221;\n     APPLICATIONNAME=&#8221;Command Line Agruments&#8221;\n     SINGLEINSTANCE=&#8221;yes&#8221;\n&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;<\/p>\n<p>Sub Window_onLoad\n    arrCommands = Split(objTestHTA.commandLine, chr(34))\n    For i = 3 to (Ubound(arrCommands) &#8211; 1) Step 2\n        Msgbox arrCommands(i)\n    Next\nEnd Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/PRE>\n<P>The part we really care about is the <B>Window_onLoad <\/B>subroutine (a subroutine which automatically runs any time the HTA is started). The first line in our subroutine uses the Split function to create an array from the value returned by the commandLine property, using the double quote mark &#8211; chr(34) &#8211; as the split character. We then use a For Next loop starting with item 3 and continuing through the last item in the array (the upper bound of the array minus 1). In this script we simply echo back the value of each argument, though obviously once you have that value in hand you can do anything you want with it.<\/P>\n<P>Just remember to surround each argument with double quotes (something you\u2019d have to do anyway if any of those arguments included spaces). If you do that, the script should work as expected. If you don\u2019t\u2026 well, if you\u2019re crazy enough to try to include command-line arguments that aren\u2019t surrounded by double quotes all we can do is wish you the best of luck (and tell you that it\u2019s been nice knowing you).<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr05\/hey0420.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr05\/hey0420.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I pass command-line variables to an HTA when it starts?&#8212; DM Hey, DM. Ok, we admit it: you almost had us on this one. (Not that being able to stump the Scripting Guys is particularly hard, mind you.) We had no idea whether you could pass command-line variables to an [&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-69983","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! How can I pass command-line variables to an HTA when it starts?&#8212; DM Hey, DM. Ok, we admit it: you almost had us on this one. (Not that being able to stump the Scripting Guys is particularly hard, mind you.) We had no idea whether you could pass command-line variables to an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69983","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=69983"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69983\/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=69983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}