我手头有问题。 我需要读取一个日志文件,如果日志文件中的任何行包含指定的日志级别,我需要在结果文件中写入该行。 我写了下面的代码,它工作得很好。 但我的问题是 - 日志文件的大小。在我的项目中,日志文件的最大大小设置为100 MB。我用2 MB - 日志文件(15512行)测试了这段代码,并且花了很长时间(约1小时15分钟)。此外,我不确定这将如何处理大文件。 你有其他方法吗?快速的帮助将非常感激。
Option Explicit
Public Function chekLogFile(sLogFileName, sLogLevelToCheck, sResultFile)
Dim oFSO, oFile, oResultFileObj, oResultFile
Dim sFileContent
Dim arrFileContent
Dim iNumberOfLinesInFile, iCounter
' Open the result file to write
Set oResultFileObj = CreateObject("Scripting.FileSystemObject")
Set oResultFile = oResultFileObj.OpenTextFile(sResultFile,8)
' Read content from log file
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile(sLogFileName,1)
sFileContent = oFile.ReadAll()
' Create an array with content of each line as its elements
arrFileContent = Split(sFileContent,vbcrlf)
' Get the number of lines
iNumberOfLinesInFile = UBound(arrFileContent)
' If the line contails the log level, write the line in the result file
' The lines we are concereed about start as follows
' 20150823135921 :::: ERROR ::
' 20150823135929 :::: WARNING ::
' 20150823135930 :::: INFO ::
' Please note: Any other occurrence of Either of the word except like above will not be counted.
For iCounter = 0 To iNumberOfLinesInFile Step 1
If Mid(arrFileContent(iCounter),21,Len(sLogLevelToCheck)) = sLogLevelToCheck Then
oResultFile.WriteLine(arrFileContent(iCounter))
End If
Next
' Close the files
oFile.Close
oResultFile.Close
' Release the objects
Set oResultFile = Nothing
Set oFile = Nothing
Set oFSO = Nothing
Set oResultFileObj = Nothing
End Function
' Log level could be either ERROR OR WARNING OR INFO
Call chekLogFile("E:\UFTTrial\gmail.log", "ERROR", "E:\UFTTrial\ResultFile.txt")
答案 0 :(得分:1)
文本文件是字符串的集合。如果你需要逐行/逐行处理它,啜饮文件然后将内容分成数组是浪费时间和内存。请改用.ReadLine()。
应用于20 MB文件的示例代码在我的(慢)机器上花了不到2分钟:
Option Explicit
Const ForAppending = 8
Const csSrcFile = "M:\lib\kurs0705\testdata\lines.txt"
Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim dtStart : dtStart = Now()
checkLogFile csSrcFile, "This", "selected.txt"
Dim dtEnd : dtEnd = Now() - dtStart
WScript.Echo oFSO.GetFile(csSrcFile).Size / 10^6, "MB ", FormatDateTime(dtEnd, vbShortTime)
Public Sub checkLogFile(sLogFileName, sLogLevelToCheck, sResultFile)
Dim oInFile : Set oInFile = oFSO.OpenTextFile(sLogFileName)
Dim oOutFile : Set oOutFile = oFSO.OpenTextFile(sResultFile, ForAppending, True)
Do Until oInFile.AtEndOfStream
Dim sLine : sLine = oInFile.ReadLine()
If Mid(sLine, 1, Len(sLogLevelToCheck)) = sLogLevelToCheck Then
oOutFile.WriteLine sLine
End If
Loop
oInFile.Close
oOutFile.Close
End Sub
输出:
cscript readlog.vbs
20,888896 MB 00:01
答案 1 :(得分:0)
文件的物理尺寸不是重要方面;文件中的行数是。行数越高,使用时间越长.ReadLine()
Ekkehard的回答几乎是我要写的内容。请记住,具有2,000行和200个字符/行的文件读取速度明显快于20,000行和20个字符/行文件。您尝试解析的文件中有多少行?