How Can I Pass Command-Line Variables to an HTA When It Starts?
Hey, Scripting Guy! How can I pass command-line variables to an HTA when it starts?
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 commandLine property and, after a bit of hacking around, came up with a solution.
Note. If you aren’t 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 HTA Developers Center.
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):
Now suppose you type something like this at the command prompt:
c:\scripts\cmdline.hta arg1 arg2
Guess what the commandLine property returns then? That’s right; the entire command string, with the path surrounded by double quotes and the two arguments tacked onto the end:
“c:\scripts\cmdline.hta” arg1 arg2
In other words, we can capture the command-line arguments; as you can see, both arg1 and arg2 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.
Our first thought was to remove the double quotes and then use the VBScript Split 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:
That worked, but there was a problem. Suppose you had a blank space in the HTA path; for example, suppose our path was C:\Scripts\My HTAs\Cmdline.hta. That “extra” blank space was a problem; if we split the string on each blank space we’d get an array that looked like this:
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:
c:\scripts\cmdline.hta “arg1” “arg2”
Why? Well, this returns a commandLine that looks like this:
“c:\scripts\cmdline.hta” “arg1” “arg2”
Each argument is now enclosed within double quotes, just like the path is. If we now split on the double quote mark (chr(34)) we’ll get an array that looks like this:
In other words, every other line in the array has data of interest to us. Why do the other lines consist of – for all intents and purposes – nothing at all? Well, to mimic the effect replace each “ in this sentence with a carriage return-linefeed and see what you get:
“c:\scripts\cmdline.hta” “arg1” “arg2”
Believe it or not, that’s OK. Because we have an array, we can use a For Next loop to cycle through all the items. Furthermore, we can use the Step parameter to loop through every other line. For example, suppose we have the following array:
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’s exactly what we’ll do in our HTA. We’re 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’t 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’ll start there, then skip to item 6. (Remember, item 5 will just be a blank space.) That’s how we’ll extract just the command-line arguments for the HTA.
Here’s the complete HTA code (remember to save this as a .HTA file and not a .VBS file):
<title>Command Line Agruments</title>
APPLICATIONNAME=”Command Line Agruments”
arrCommands = Split(objTestHTA.commandLine, chr(34))
For i = 3 to (Ubound(arrCommands) – 1) Step 2
The part we really care about is the Window_onLoad 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 – chr(34) – 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.
Just remember to surround each argument with double quotes (something you’d have to do anyway if any of those arguments included spaces). If you do that, the script should work as expected. If you don’t… well, if you’re crazy enough to try to include command-line arguments that aren’t surrounded by double quotes all we can do is wish you the best of luck (and tell you that it’s been nice knowing you).