带有 HTA 前端的 VBScript

Posted

技术标签:

【中文标题】带有 HTA 前端的 VBScript【英文标题】:VBScript with HTA frontend 【发布时间】:2013-04-09 19:21:58 【问题描述】:

好的,我有一个漂亮的 VBS,它会在巨大的日志文件中搜索某些字符串,但我并不总是想在每个日志文件中搜索每个字符串。我想要一个允许最终用户选择他们想要查找的字符串的 HTA 前端。

这是我的代码示例,它可以作为 vb 很好地工作,但在这个示例中,我想要牛、山羊、猫、狗等的复选框,并且无论脚本如何正确运行许多被选中..(我的实际脚本有大约 20 个单词可供选择)并且“动物日志文件”的路径和名称目前是一个输​​入框..我也希望在 hta 中使用它。

Const ForReading = 1
Dim words(7)
Dim msg
words(0) = "cows"
words(1) = "goats"
words(2) = "cats"
words(3) = "dogs"
words(4) = "elephants"
words(5) = "giraffes"   


Set objFSO = CreateObject("Scripting.FileSystemObject")

strAnswer = InputBox("Please enter the path & filename for the animal log file:", _
    "Create File")
Wscript.Echo strAnswer

Set objFile = objFSO.OpenTextFile( strAnswer, ForReading)
Set inFile = objFSO.OpenTextFile ( strAnswer, ForReading)


strContents = objFile.ReadAll
objFile.Close

Set outFile = objFSO.OpenTextFile( strAnswer &"_parsed-output.txt", 8, True)

Do Until inFile.AtEndOfStream
    strSearchString = inFile.ReadLine
    For i = 0 To UBound(words)-1
    If InStr(strSearchString,words(i)) Then
        msg = msg&strSearchString&vbcrlf
    End If
    next
Loop

inFile.Close
outfile.WriteLine msg

WScript.Echo "Done!"

【问题讨论】:

【参考方案1】:

这可以帮助您入门。如果选择了多个复选框,您将需要编写如何处理以及打开这些日志文件(多个日志文件)所需的代码逻辑。您可以在此处找到有关 HTA 的更多信息,http://technet.microsoft.com/en-us/scriptcenter/dd742317.aspx

<html>
<head>
<title>My Logfile App</title>
<HTA:APPLICATION
  APPLICATIONNAME="My Logfile App"
  ID="MyLogfileApp"
  VERSION="1.0"/>
</head>

<script language="VBScript">

Sub Window_OnLoad
  window.resizeto 300,300
End Sub

Sub Start_Button()

    Const ForReading = 1
    Dim objFSO, objFile, inFile, strAnswer
    strAnswer = ""

    If chkCows.Checked Then strAnswer = "Cows"
    If chkGoats.Checked Then strAnswer = "Goats"
    If chkCats.checked Then strAnswer = "Cats"
    If chkDogs.Checked Then strAnswer = "Dogs"
    If chkElephants.Checked Then strAnswer = "Elephants"
    If chkGiraffes.Checked Then strAnswer = "Giraffes"

    'If strAnswer is empty then nothing was checked.
    If strAnswer = "" Then 
        Window.Alert "Please Make an Selection!"
        Exit Sub
    End If

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile( strAnswer, ForReading)
    Set inFile = objFSO.OpenTextFile ( strAnswer, ForReading)

    strContents = objFile.ReadAll
    objFile.Close

    Set outFile = objFSO.OpenTextFile( strAnswer &"_parsed-output.txt", 8, True)

    Do Until inFile.AtEndOfStream
        strSearchString = inFile.ReadLine
        For i = 0 To UBound(words)-1
        If InStr(strSearchString,words(i)) Then
            msg = msg&strSearchString&vbcrlf
        End If
        next
    Loop

    inFile.Close
    outfile.WriteLine msg

    Window.Alert "Done!"
End Sub
</script>

<body bgcolor="white">
<center>
    <label>Choose your logfile below.</label><br />
</center>
<input type="checkbox" name="chkCows" id="chkCows">Cows<br />
<input type="checkbox" name="chkGoats" id="chkGoats">Goats<br />
<input type="checkbox" name="chkCats" id="chkCats">Cats<br />
<input type="checkbox" name="chkDogs" id="chkDogs">Dogs<br />
<input type="checkbox" name="chkElephants" id="chkElephants">Elephants<br />
<input type="checkbox" name="chkGiraffes" id="chkGiraffes">Giraffes<br />
<p>
<center>
    <input type="button" name="btnStart" id="btnStart" value="Start" onclick="Start_Button">
</center>
</body>
</html>

【讨论】:

以上是关于带有 HTA 前端的 VBScript的主要内容,如果未能解决你的问题,请参考以下文章

ProgressBar 在 VBScript HTA 中没有得到刷新

HTA - 多HTA之间切换

javascript HierarchicalTaskAnalysis-hta编程(.hta)

在 vbscript 中使用 HTA

HTA 中的 JavaScript 版本

HTA 游戏手柄 API(或替代方案)