Vbscript 用于扫描最新日志文件以查找错误的VB脚本

Vbscript 用于扫描最新日志文件以查找错误的VB脚本,vbscript,Vbscript,我有一个VB脚本,它扫描提到的日志文件中的错误,并通过电子邮件发送通知 如何扫描文件夹中的最新日志文件?例如,Filename1.070615(Filename1.mmddyy)是一个日志文件。达到一定大小后,日志文件将切换到具有相同名称但不同日期的新文件:Filename1.070615 cdoSendUsingPort = 2, _ Const ForReading = 1 Dim intStartAtLine, strFileCreateddate, i, strResults

我有一个VB脚本,它扫描提到的日志文件中的错误,并通过电子邮件发送通知

如何扫描文件夹中的最新日志文件?例如,
Filename1.070615
Filename1.mmddyy
)是一个日志文件。达到一定大小后,日志文件将切换到具有相同名称但不同日期的新文件:
Filename1.070615

    cdoSendUsingPort = 2, _

Const ForReading = 1

Dim intStartAtLine, strFileCreateddate, i, strResults, strTextToScanFor, bStartFromScratch
Dim strLastCheckedFor, strArrayToString, strSubject, strMailFrom, strMailTo

 strMailto               = "<Emailaddress>"


strMailFrom     = "<FromAddress>"      

strSubject              = "Log scanner"


strSMTPServer   = "x.x.x.x"    

FileToRead              = "D:\LOG\filename1.mmddyy"

arrTextToScanFor        = Array("error","another thing")

Set WshShell            = WScript.CreateObject("WScript.Shell")

searchkey                       = replace(replace(filetoread,":",""),"\","_")

On Error Resume Next
strLastFileCheckedCreateDate    = WshShell.RegRead("HKCU\Software\RDScripts\CheckTXTFile\" & searchkey & "\CreateDate")
strLastFileLastLineChecked              = WshShell.RegRead("HKCU\Software\RDScripts\CheckTXTFile\" & searchkey & "\LastLineChecked")
strLastCheckedFor                               = WshSHell.RegRead("HKCU\Software\RDScripts\CheckTXTFile\" & searchkey & "\CheckForString")
iLastCheckedLine                                = WshSHell.RegRead("HKCU\Software\RDScripts\CheckTXTFile\" & searchkey & "\LastLineChecked")
On Error GoTo 0

Set objFSO                                              = WScript.CreateObject("Scripting.FileSystemObject")
Set varFile                                     = objFSO.GetFile(FileToRead)

arrLastCheckedForText                   = split(strLastCheckedFor,",")
strFileCreateDate                               = varfile.datecreated
strFileModifiedDate                             = varfile.datelastmodified

sStatus                                                 = "<li>Using mail server: " & strSMTPServer & "</li><li>Running from: " & wscript.scriptfullname & "</li>"

Set objTextFile = objFSO.OpenTextFile(FileToRead, ForReading)
objTextFile.ReadAll

    iLineCount = objTextFile.Line

objTextFile.close


    If strLastCheckedFor = "" Then
        bStartFromScratch = true
        sStatus = sStatus & "<li>First run of script against string search</li>" & vbcrlf

ElseIf ubound(arrTextToScanFor) <> ubound(arrLastCheckedForText) Then
        bStartFromScratch = true
        sStatus = sStatus & "<li>Count of string search criteria has changed</li>" & vbcrlf
Else
        For each strItem in arrTextToScanFor
                                    Else   
                        bStartFromScratch = true

                        'MsgBox strResults
                End If

                If bStartFromScratch = true Then
                        sStatus = sStatus & "<li>String search criteria does not match prior search</li>" & vbcrlf
                End If

        Next
End If
If cint(iLineCount) < cint(iLastCheckedLine) Then      
        bStartFromScratch = true
        sStatus = sStatus & "<li>Last line checked (" & iLastCheckedLine & ") is greater than total line count (" & iLineCount & ") in file</li>"

End If

If CStr(strFileCreateDate) = CStr(strLastFileCheckedCreateDate) and bStartFromScratch <> true Then

        intStartAtLine = strLastFileLastLineChecked
        If bStartFromScratch <> true then
                sStatus = sStatus & "<li>Continuing search from line " & intStartAtLine & "</li>" & vbcrlf
        End If  
ElseIf strFileCreateDate <> strLastFileCheckedCreateDate or bStartFromScratch = true Then

        intStartAtLine = 0
        If bStartFromScratch <> true then
                sStatus = sStatus & "<li>File created date has changed, starting search from line 0</li>" & vbcrlf
        End If
End If

i = 0
Dim strNextLine

For each strItem in arrTextToScanFor
        strArrayToString = strArrayToString & delim & strItem
        delim = ","
Next


Set objTextFile = objFSO.OpenTextFile(FileToRead, ForReading)

Do While objTextFile.AtEndOfStream <> True
      If i < CInt(intStartAtLine) Then
        objTextFile.skipline
      Else
        'MsgBox i
        strNextLine = objTextFile.Readline
        For each strItem in arrTextToScanFor
                If InStr(LCase(strNextLine),LCase(strItem)) Then
                        strResults = "<span style='font-family:courier-new;color:#696969'><span style='font-weight:bold;background-color:#BEF3F3'>Line " & i & ":</span> " & replace(lcase(strNextLine),lcase(strItem),"<span style='background-color:#FFFF81'>" & strItem & "</span>") & "</span><br>" & vbcrlf & strResults
                        bSendMail = true
                End If
        Next
      End If   
      i = i + 1

Loop
objTextFile.close

Set WshShell = CreateObject("WScript.Shell")
'Let's save our settings for next time.
WshShell.RegWrite "HKCU\Software\RDScripts\CheckTXTFile\" & searchkey & "\FileChecked", FileToRead, "REG_SZ"
WshShell.RegWrite "HKCU\Software\RDScripts\CheckTXTFile\" & searchkey & "\CreateDate", strFileCreateDate, "REG_SZ"
WshShell.RegWrite "HKCU\Software\RDScripts\CheckTXTFile\" & searchkey & "\LastLineChecked", i, "REG_SZ"
WshShell.RegWrite "HKCU\Software\RDScripts\CheckTXTFile\" & searchkey & "\LastScanned", Now, "REG_SZ"
WshShell.RegWrite "HKCU\Software\RDScripts\CheckTXTFile\" & searchkey & "\CheckForString",strArrayToString, "REG_SZ"
set WshShell = nothing

    strFileSummary                          = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>File path:</td><td>" & FileToRead & "</td></tr>"
strFileCreateDateSummary        = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>Created date:</td><td>" & strFileCreateDate & "</td></tr>"
strFileModifiedDateSummary      = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>Modified date:</td><td>" & strFileModifiedDate & "</td></tr>"
strArraySummary                         = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>Text string(s):</td><td>" & strArrayToString & "</td></tr>"
strFileLineSummary                      = "<tr><td style='font-family:calibri;font-weight:bold;color:darkblue;width:200px'>Last line checked:</td><td>" & i & "</td></tr>"
strSummary                                      = strFileSummary & strFileCreateDateSummary & strFileModifiedDateSummary & strArraySummary & strFileLineSummary
strBodyContent                          = "<table style='font-family:calibri;'>" & strSummary & "</table><br><br><span style='font-size:large;'>Entries:</span><br>" & strResults & "<div style='padding-top:30px;font-size:x-small'><br><div style='font-weight:bold;font-family:calibri;color:black;'>Job Details:<ul style='font-weight:normal;font-family:calibri;color:darkgray;'>" & sStatus & "</ul></div></div>"
on error goto 0

'Send the email if need be.
If bSendMail = true Then Call sendmail(strMailFrom,strMailTo,strSubject,strBodyContent)

'------------------------------------------------------------------------
'Function EmailFile - email the warning file
'------------------------------------------------------------------------
Function SendMail(strFrom,strTo,strSubject,strMessage)
Dim iMsg, iConf, Flds
On Error GoTo 0

'//  Create the CDO connections.
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields


'//  SMTP server configuration.
With Flds
    .Item(cdoSendUsingMethod) = cdoSendUsingPort

    '//  Set the SMTP server address here.
   .Item(cdoSMTPServer) = strSMTPServer
    .Update
End With

'//  Set the message properties.
With iMsg
    Set .Configuration = iConf
        .To       = strMailTo
        .From     = strMailFrom
        .Subject  = strSubject
        .htmlbody = strMessage
End With

'iMsg.HTMLBody = strMessage

'//  Send the message.

iMsg.Send ' send the message.

If CStr(err.number) <> 0 Then

Else

End If
End Function
cdosensingport=2_
常数ForReading=1
Dim IntStartLine、strFileCreateddate、i、strResults、strTextToScanFor、bStartFromScratch
Dim strLastCheckedFor、strArrayToString、strSubject、strMailFrom、strMailTo
strMailto=“”
strMailFrom=“”
strSubject=“日志扫描程序”
strSMTPServer=“x.x.x.x”
FileToRead=“D:\LOG\filename1.mmddyy”
arrTextToScanFor=Array(“错误”,“另一件事”)
设置WshShell=WScript.CreateObject(“WScript.Shell”)
searchkey=replace(replace(filetoread,“:”,“),“\”,“\”)
出错时继续下一步
strLastFileCheckedCreateDate=WshShell.regrad(“HKCU\Software\RDScripts\CheckTXTFile\”&searchkey&“\CreateDate”)
strLastFileLastLineChecked=WshShell.regrad(“HKCU\Software\RDScripts\CheckTXTFile\”&searchkey&“\LastLineChecked”)
strLastCheckedFor=WshSHell.regrad(“HKCU\Software\RDScripts\CheckTXTFile\”&searchkey&“\CheckForString”)
iLastCheckedLine=WshSHell.regrad(“HKCU\Software\RDScripts\CheckTXTFile\”&searchkey&“\LastLineChecked”)
错误转到0
设置objFSO=WScript.CreateObject(“Scripting.FileSystemObject”)
Set varFile=objFSO.GetFile(FileToRead)
arrLastCheckedForText=split(strLastCheckedFor,“,”)
strFileCreateDate=varfile.datecreated
strFileModifiedDate=varfile.datelastmodified
sStatus=“
  • 使用邮件服务器:&strSMTPServer&”
  • 从运行:&wscript.scriptfullname&“
  • ” 设置objTextFile=objFSO.OpenTextFile(FileToRead,ForReading) objTextFile.ReadAll iLineCount=objTextFile.Line objTextFile.close 如果strLastCheckedFor=“”,则 bStartFromScratch=true sStatus=sStatus&“
  • 针对字符串搜索的第一次脚本运行”
  • ”&vbcrlf ElseIf ubound(ArrTextToScan for)ubound(arrLastCheckedForText)然后 bStartFromScratch=true sStatus=sStatus&“
  • 字符串搜索条件的计数已更改”
  • ”&vbcrlf 其他的 对于arrTextToScanFor中的每个横档 其他的 bStartFromScratch=true 'MsgBox strResults 如果结束 如果bStartFromScratch=true,则 sStatus=sStatus&“
  • 字符串搜索条件与先前的搜索不匹配”
  • ”&vbcrlf 如果结束 下一个 如果结束 如果cint(iLineCount)最后检查的行(&iLastCheckedLine&”)大于文件中的总行数(&iLineCount&)” 如果结束 如果CStr(strFileCreateDate)=CStr(strlLastFileCheckedCreateDate)和bStartFromScratch为true,则 intStartAtLine=strLastFileLastLineChecked 如果bStartFromScratch为真,则 sStatus=sStatus&“
  • 继续从行搜索”&intStartAtLine&“
  • ”&vbcrlf 如果结束 ElseIf strFileCreateDate strLastFileCheckedCreateDate或bStartFromScratch=true则 INTSTARTALINE=0 如果bStartFromScratch为真,则 sStatus=sStatus&“
  • 文件创建日期已更改,从第0行开始搜索”&vbcrlf 如果结束 如果结束 i=0 暗斜线 对于arrTextToScanFor中的每个横档 strArrayToString=strArrayToString&delim&strItem delim=“,” 下一个 设置objTextFile=objFSO.OpenTextFile(FileToRead,ForReading) 当objTextFile.AtEndOfStream为True时执行此操作 如果我”&vbcrlf&strResults bSendMail=true 如果结束 下一个 如果结束 i=i+1 环 objTextFile.close 设置WshShell=CreateObject(“WScript.Shell”) '让我们保存设置以备下次使用。 WshShell.RegWrite“HKCU\Software\RDScripts\CheckTXTFile\”&searchkey&“\FileChecked”,FileToRead,“REG_SZ” WshShell.RegWrite“HKCU\Software\RDScripts\CheckTXTFile\”&searchkey&“\CreateDate”,strFileCreateDate,“REG_SZ” WshShell.RegWrite“HKCU\Software\RDScripts\CheckTXTFile\”&searchkey&“\LastLineChecked”,i,“regu SZ” WshShell.RegWrite“HKCU\Software\RDScripts\CheckTXTFile\”&searchkey&“\lastscanted”,现在是“REG_SZ” WshShell.RegWrite“HKCU\Software\RDScripts\CheckTXTFile\”&searchkey&“\CheckForString”,strArrayToString,“REG_SZ” 设置WshShell=nothing strFileSummary=“文件路径:&FileToRead&” strFileCreateDateSummary=“创建日期:&strFileCreateDate&” strFileModifiedDateSummary=“修改日期:&strFileModifiedDate&” strArraySummary=“文本字符串:”&strArrayToString&“ strFileLineSummary=“选中的最后一行:”&i&“ strSummary=strFileSummary&strFileCreateDateSummary&strFileModifiedDateSummary&strArraySummary&strFileLineSummary strBodyContent=”“&strSummary&“

    条目:
    ”&strResults
    Set re = New RegExp
    re.Pattern = "^(filename1\.)(\d{2})(\d{2})(\d{2})$"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    For Each objFile In fso.GetFolder("d:\log").Files
        If re.Test(objFile.Name) Then
            strCompareName = re.Replace(objFile.Name, "$1$4$2$3")
            If strCompareName > strLatest Then strLatest = strCompareName
        End If
    Next
    
    ' Switch the name back...
    strLatest = re.Replace(strLatest, "$1$3$4$2")
    WScript.Echo "The latest file is: " & strLatest
    
    strCompareName = re.Replace(objFile.Name, "$1$4$2$3")
    
     strLatest = re.Replace(strLatest, "$1$3$4$2")