我有一个包含日志数据的文本文件。我从日志文件中检索序列号,输出状态,引脚号等。
我的日志文件如下所示。它有很多内容。
"FORMAT=U2"
"BEGIN HEADER"
"TS=600581"
"ST=1001038"
"TC=60055"
"PIN=100577"
"SN=GXT220.1"
我必须读取文件,打印PIN为100577,序列号为GXT220.1。我对may文件做了同样的事情,并且所有文件的行号都不相同。
答案 0 :(得分:0)
使用
如:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim s : s = goFS.OpenTextFile("43009791.txt").ReadAll()
Dim r : Set r = New RegExp
r.Global = True
r.Multiline = True
r.Pattern = "^""(\w+)=([^""]+)"""
Dim d : Set d = CreateObject("Scripting.Dictionary")
Dim m
For Each m In r.Execute(s)
d(m.Submatches(0)) = m.Submatches(1)
Next
Dim k
For Each k In d.Keys()
WScript.Echo k, "=>", d(k)
Next
输出:
cscript 43009791.vbs
FORMAT => U2
TS => 600581
ST => 1001038
TC => 60055
PIN => 100577
SN => GXT220.1
答案 1 :(得分:0)
不确定这是否是你想要的--->读取文本文件并在具有常量指示符的行中提取特定信息。
如果是这样,这就是我要做的。
Option Explicit
Dim FSO, ReadTextFile ' object
Dim Textline ' string/variant
Dim PinNumber, Serial ' Output
FSO = CreateObject("Scripting.FileSystemObject")
Set ReadTextFile = FSO.OpenTextFile("File.txt", 1) ' Open text with read only mode
Do Until ReadTextFile.AtEndOfStream
Textline = ReadTextFile.Readline()
If Instr(Textline, "PIN=") Then ' If textline contain string "PIN="
PinNumber = Split(Textline, "=")(1) ' Split the textline with "=" indicator
End If
If Instr(Textline, "SN=") Then
Serial = Split(Textline, "=")(1)
End If
Loop ' Read through every line
此示例仅在一个文件中只有一个指示符时才有效。并且您的指标与所有txt文件相同。希望它有所帮助:)