2013-04-09 39 views
0

好的,我有一個漂亮的VBS,它將搜索特定字符串的大型日誌文件,但我並不總是希望爲每個字符串搜索每個日誌文件。我想要一個HTA前端,允許最終用戶選擇他們想要查找的字符串。具有HTA前端的VBScript

這裏是我的代碼示例,它的工作原理很棒,但在本例中,我想爲牛,山羊,貓,狗等複選框。並且腳本無論正確運行有多少選擇..(我的實際腳本有大約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>