VB脚本扫描最新的日志文件的错误



我有一个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:LOGfilename1.mmddyy"
arrTextToScanFor        = Array("error","another thing")
Set WshShell            = WScript.CreateObject("WScript.Shell")
searchkey                       = replace(replace(filetoread,":",""),"","_")
On Error Resume Next
strLastFileCheckedCreateDate    = WshShell.RegRead("HKCUSoftwareRDScriptsCheckTXTFile" & searchkey & "CreateDate")
strLastFileLastLineChecked              = WshShell.RegRead("HKCUSoftwareRDScriptsCheckTXTFile" & searchkey & "LastLineChecked")
strLastCheckedFor                               = WshSHell.RegRead("HKCUSoftwareRDScriptsCheckTXTFile" & searchkey & "CheckForString")
iLastCheckedLine                                = WshSHell.RegRead("HKCUSoftwareRDScriptsCheckTXTFile" & 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 "HKCUSoftwareRDScriptsCheckTXTFile" & searchkey & "FileChecked", FileToRead, "REG_SZ"
WshShell.RegWrite "HKCUSoftwareRDScriptsCheckTXTFile" & searchkey & "CreateDate", strFileCreateDate, "REG_SZ"
WshShell.RegWrite "HKCUSoftwareRDScriptsCheckTXTFile" & searchkey & "LastLineChecked", i, "REG_SZ"
WshShell.RegWrite "HKCUSoftwareRDScriptsCheckTXTFile" & searchkey & "LastScanned", Now, "REG_SZ"
WshShell.RegWrite "HKCUSoftwareRDScriptsCheckTXTFile" & 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

如果您的日志文件命名为filename1.yymmdd,则会容易一些。

然而,我们不仅可以使用正则表达式来验证文件名,还可以交换日期组件,将它们放在我们想要的格式中!

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")

将格式从mmddyy更改为yymmdd,并将其保存在字符串中以供比较。

一旦我们完成了比较,我们只需要取我们找到的最新文件并反转这个过程:

 strLatest = re.Replace(strLatest, "$1$3$4$2")

获取原来的文件名!

最新更新