我需要在日志文件中查找文本字符串。
找到该文本字符串后,我想立即获取接下来的 4 行文本,请参见下面的示例:
Example found text string
first line after found string
second line after found string
third line after found string
forth line after found string
我的代码是:
'strFileName = "D:write123.xls"
'Set objExcel = CreateObject("Excel.Application")
'objExcel.Visible = True
'Set objWorkbook = objExcel.Workbooks.Add()
'objWorkbook.SaveAs(strFileName)
'objExcel.Quit
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("D:mic.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileToWrite = objFSO.OpenTextFile("D:write123.xlsx", 2)
objFileToWrite.Write strFileText
objFileToWrite.Close
任何帮助或任何替代方案或任何脚本将不胜感激,我只想将日志文件的内容提取到我的目标表中。
谢谢奇拉格
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
dim objFileToRead : Set objFileToRead = objFSO.OpenTextFile("D:mic.txt",1)
dim myArray(0)
While Not objFileToRead.AtEndOfStream
strLine = objFileToRead.ReadLine
myArray(UBound(myArray)) = strLine
reDim Preserve myArray(UBound(myArray)+1)
Wend
objFileToRead.Close
Dim objExcelApp : Set objExcelApp = CreateObject("Excel.Application")
dim myExcel : Set myExcel = objExcel.Workbooks.Add()
With myExcel.Worksheets(1)
For myLoop = 0 to UBound(myArray)
.Range("A" & myLoop).Value = myArray(myLoop)
Next
End With
myExcel.SaveAs Filename:="D:write123.xlsx"
myExcel.Close
objExcel.Quit
Set objExcel = Nothing
Set objFileToRead = Nothing
Set objFSO = Nothing
此代码的作用是将日志文件的全部内容读入数组;创建 Excel 和工作簿的实例;对于日志条目数组中的每一行,将其输入到"A"列中;在退出 Excel 之前保存并关闭工作簿。 如果您需要更多关于这里发生的事情的解释,请告诉我?