【发布时间】:2016-01-05 04:58:46
【问题描述】:
我想从本地机器中的文本格式的日志文件中获取时间戳值,并使用 VB 脚本将这些值保存到 Excel 文件中。
我的日志文件格式是:-
14.000.00.10 - - [07/Mar/2015:16:06:51 -0800] “GET /twiki/bin/rdiff/TWiki/NewUserTemplate?rev1=1.3&rev2=1.2 HTTP/1.1”200 4523 14.000.00.10 - - [07/Mar/2015:16:10:02 -0800] “GET /mailman/listinfo/hsdivision HTTP/1.1”200 6291 14.000.00.10 - - [07/Mar/2015:16:11:58 -0800] “GET /twiki/bin/view/TWiki/WikiSyntax HTTP/1.1”200 7352 14.000.00.10 - - [07/Mar/2015:16:20:55 -0800] “GET /twiki/bin/view/Main/DCCAndPostFix HTTP/1.1”200 5253 14.000.00.10 - - [07/Mar/2015:16:23:12 -0800] “GET /twiki/bin/oops/TWiki/AppendixFileSystem?template=oopsmore¶m1=1.12¶m2=1.12 HTTP/1.1”通过获取在多行中重复出现的 ID 值,我如何保存我的时间戳,例如[07/Mar/2015:16:23:12]从日志.txt文件到Excel文件?
我尝试编写此代码:
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbooks.Add
Set ws = wb.Sheets(1)
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\access_log.txt")
strContents = objFile.ReadAll
objFile.Close
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
objRegEx.Pattern = "mailman"
Set colMatches = objRegEx.Execute(strContents)
For Each Match in colMatches
strReturnStr = "Match found at position "
strReturnStr = strReturnStr & match.FirstIndex & ". Match Value is '"
StrReturnStr = strReturnStr & match.value & "'." & "<BR>" & VBCrLf
WScript.Echo(strReturnStr)
Next
wb.SaveAs "E:\access_og.csv", -4143, , , , False
wb.Close
xl.Quit
在 cmd 提示符下使用 cscript name.vbs 运行时,它会显示找到字符串的行号,然后 .csv 文件打开时出现错误“'access_og.csv' 的文件格式和扩展名不匹配。文件可能已损坏且不安全。"
问题还是没有解决:(
【问题讨论】: