VBScript - 将参数传递给 HTA

Kon*_*nai 2 variables vbscript hta

我正在 VBScript 中编写一些脚本,我需要将一些变量值传递到 HTA,我将用作前端来显示正在发生更新。

我该怎么做呢?

VBScript-------

TestVar1 = "Something 1"
TestVar2 = "Something 2"

wshShell.Run "Updater.hta " & TestVar1 & TestVar2
Run Code Online (Sandbox Code Playgroud)

然后

HTA------- 

TestVar1 = Something.Arguments(0)
TestVar2 = Something.Arguments(1)

Msgbox TestVar1
Msgbox TestVar2
Run Code Online (Sandbox Code Playgroud)

我意识到这不是完全正确的代码,我只是放置它来说明我正在尝试做什么。

你们能提供解决这个问题的任何帮助就太好了,谢谢!

Bon*_*ond 5

将您的参数括在引号中。由于 VBScript 使用"字符串文字,因此您需要通过将其加倍来转义"",或者可以使用该Chr()函数来指定引号字符:

TestVar1 = "Something 1"
TestVar2 = "Something 2"

Dim strParams
strParams = strParams & " " & Chr(34) & TestVar1 & Chr(34)
strParams = strParams & " " & Chr(34) & TestVar2 & Chr(34)

wshShell.Run "updater.hta" & strParams
Run Code Online (Sandbox Code Playgroud)

在您的 HTA 中,该Arguments集合不可用。相反,您必须解析CommandLineHTA 对象的属性。在这种情况下,CommandLine您的 HTA 收到的消息将如下所示:

TestVar1 = "Something 1"
TestVar2 = "Something 2"

Dim strParams
strParams = strParams & " " & Chr(34) & TestVar1 & Chr(34)
strParams = strParams & " " & Chr(34) & TestVar2 & Chr(34)

wshShell.Run "updater.hta" & strParams
Run Code Online (Sandbox Code Playgroud)

所以你有两种选择来检索你的参数。您可以使用正则表达式来获取引号内的所有内容,也可以使用Split()引号CommandLine。如果您的参数之一有引号,事情会变得更加棘手,您可能需要考虑使用不同的字符来括住您的参数。

这是一个用于Split()提取参数的 HTA 框架:

<head>
    <HTA:APPLICATION
        ID="myhta" 
        APPLICATIONNAME="HTA Test"
    >
</head>

<script language="VBScript">
    Sub Window_OnLoad()
        a = Split(myhta.CommandLine, Chr(34))
        MsgBox "Arg 1 = " & a(3)
        MsgBox "Arg 2 = " & a(5)
    End Sub
</script>
Run Code Online (Sandbox Code Playgroud)

当您使用 时Split(),您将得到类似以下内容的信息:

a = Split(myhta.CommandLine, Chr(34))
' a(0) = <blank>
' a(1) = "updater.hta"
' a(2) = " "
' a(3) = "Something 1"
' a(4) = " "
' a(5) = "Something 2"
' a(6) = <blank>
Run Code Online (Sandbox Code Playgroud)

所以a(3)成为你的第一个论点,也a(5)成为你的第二个论点。

如果你想使用正则表达式,它会变成:

Sub Window_OnLoad()

    With New RegExp
        .Global = True
        .Pattern = """([^""]+)"""
        Set c = .Execute(myhta.CommandLine)
    End With

    For i = 1 To c.Count - 1        ' Skip i = 0 (the HTA filename)
        MsgBox "Arg " & i & " = " & c(i).SubMatches(0)
    Next

End Sub
Run Code Online (Sandbox Code Playgroud)

这将显示:

"updater.hta" "Something 1" "Something 2"
Run Code Online (Sandbox Code Playgroud)

  • @cup - 你可以。我实际上在上面的正则表达式模式中使用了它。更容易打字,但也更容易出错。 (2认同)