Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get an HTA file to accept commandline parameters.

2,323 views
Skip to first unread message

MarceepooNu

unread,
Jan 20, 2010, 6:37:01 PM1/20/10
to
I cannot figure out how to get an HTA file to accept commandline parameters.

The command line parameters ("arg1" and "arg2") do not pass through, when I
try to follow the examples provided in a Hey Scripting Guy column located at
this url:

http://blogs.technet.com/heyscriptingguy/archive/2005/04/20/how-can-i-pass-command-line-variables-to-an-hta-when-it-starts.aspx

I modified the ScriptingGuys' example as shown below, in the hopes that I'd
reveal what the Hta script was doing with the two parameters ("arg1" and
"arg2").

My modifications reveal that: Ubound(arrCommands) = 2
arrCommands(0) and arrCommands(2) are both empty,
and arrCommands(1) = C:\Scripts\CmdLine.hta

If anyone has any suggestions about where I could learn how to solve this
problem, I would appreciate it very much.

The commandline (drawn from the Hey Scripting Guy Column) that generated the
error message is:
cmdline.hta "arg1" "arg2"

The code for the hta is:
<html>
<head>
<title>objTestHTA</title>
<HTA:APPLICATION
APPLICATIONNAME="objTestHTA"
ID="objTestHTA"
SINGLEINSTANCE="yes"
VERSION="1.0"/>
<head>

</head>

<script language="VBScript">

FullName = replace(objTestHTA.commandLine,chr(34),"")
arrFN=split(FullName,"\")
FileName = Trim(arrFN(ubound(arrFN)))
SourceDir=replace(FullName,FileName,"")

Sub Window_OnLoad
arrCommands = Split(objTestHTA.commandLine, chr(34))
' For i = 0 to (Ubound(arrCommands) - 1) Step 2
For i = 0 to (Ubound(arrCommands))
Msgbox "i = " & i & vbCrLf & _
" arrCommands(i) = " & arrCommands(i) & vbCrLf & _
"Ubound(arrCommands) = " & Ubound(arrCommands)& vbCrLf
Next
me.close
End Sub

</script>

<body bgcolor="white">
</body>
</html>

MarceepooNu
--
MarceepooNu

LikeToCode

unread,
Jan 29, 2010, 4:28:04 PM1/29/10
to
Hi,
I was able to make the code work. this is the commandline I used:
"C:\Program Files\My Scripts\testHTA.hta" "arg1" "arg2"

The args (arg1 & arg2) is passed to the command line as a string. Simply use
a Select...Case statement to grab the "strings" arg1 & arg2 and assign them a
value. To test this I added two messageboxs to reveal the value assigned to
each arg.

I'm not sure what you were trying to reveal in your example. If you simply
would have replaced your entire "msgbox" with "msgbox arrcommands(i)" and
run the HTA like above you would get 7 messageboxs starting with a blank box.
This is why the scripting guys used "For i = 3", that will cut our the first
3 messageboxs revealing the first "arg1". Then they added "- 1" to remove the
last blank messagebox and a "Step 2" to skip over the middle blank messagebox
leaving you with two messagboxs "arg1" and "arg2". Replace your Window_Onload
with the code below and run your hta like above.

Sub Window_OnLoad
arrCommands = Split(TestHTA.commandLine, chr(34))
For i = 3 to (Ubound(arrCommands) - 1) Step 2
Select Case arrCommands(i)
Case "arg1"
myarg1 = "This is argument 1."
Case "arg2"
myarg2 = "This is argument 2."
End Select
Next
MsgBox myarg1
MsgBox myarg2
me.close
End Sub


0 new messages