格式2的文件解析函数。
程序代码:
Option Explicit
Public Const SiteStartStr = "Site 1:"
Public Const SiteEndStr = "Testflow ended after"
Public Const SiteBinstr1 = "Site 1 has PASSED. Binned to bin"
Public Const SiteBinstr2 = "Site 1 has FAILED. Binned to bin"
Public Enum PassFaiL
PASS = 1
FAIL = 2
End Enum
Public Enum 清显示范围
全部 = 0
分析 = 1
删除 = 2
修改 = 3
End Enum
Public Type SiteType
Site As String 'Site
SiteNew As String 'Site新值
SiteS As Long '所处行数
SiteBin As Long 'PASS还是Fail内容
SiteBinS As Long 'PASS还是Fail所在行数
PF As PassFaiL '这段是 pass 还是 Fail
End Type
Public Type FileTYpe
'文件属性
Name As String
Path As String
PathName As String
Size As Long
DateTime As String
ListCount As Long
data() As String '文件数据
Datacount As Long '总行数
'文件头内容
FileT(2) As String
'Level lot started on
'Level device started on
'数据组数
Count As Long
'数据
Site() As SiteType
PassCount As Long '统计
FailCount As Long
End Type
Public PF As FileTYpe
Public Sub 文件解析() '不传参数,填充全局变量 PF
'不负责备份文件
Dim i As Long, j As Long
Dim k As Long, tmp1 As String
Dim k1 As Long, k2 As Long
Dim fr As Long
fr = FreeFile
With PF
.Datacount = 0
.Count = 0
.FailCount = 0
.PassCount = 0
.PathName = .Path & "\" & .Name '文件名及全路径
.DateTime = FileDateTime(.PathName) '文件日期
.Size = FileLen(.PathName) '文件大小
'预处理完成
Call Form1.进度条(0.05, "准备打开文件..")
Open .PathName For Input As #fr '打开文件
tmp1 = Input(LOF(fr), #fr) '一次性读入内存
Call Form1.进度条(0.1, "打开文件完成,准备分行")
.data = Split(tmp1, vbCrLf)
Call Form1.进度条(0.15, "分行完成,开始分析")
Close #fr
.Datacount = UBound(.data)
.Count = 0
Call Form1.显示日志("正在分析步骤一..")
'因为文件头比较简单,所以不放到循环里做,直接写二组重复的语句分析掉
'Level lot started on
i = InStr(1, tmp1, "Level lot started on")
If i > 0 Then
j = InStr(i, tmp1, vbCrLf)
i = i + Len("Level lot started on")
.FileT(1) = Mid(tmp1, i, j - i)
End If
'Level device started on
i = InStr(1, tmp1, "Level device started on")
If i > 0 Then
j = InStr(i, tmp1, vbCrLf)
i = i + Len("Level device started on")
.FileT(2) = Mid(tmp1, i, j - i)
End If
tmp1 = "" '清掉内存
For j = 0 To .Datacount
If InStr(1, .data(j), SiteStartStr) = 1 Then 'Device#:
.Count = .Count + 1
End If
Next j
Call Form1.显示日志("正在分析步骤二..")
i = 0
ReDim .Site(.Count)
For j = 0 To .Datacount
If j Mod 10 = 0 Then '每处理10个变化一次进度条
Call Form1.进度条(0.58 + j / .Datacount * 0.42)
End If
k = InStr(1, .data(j), SiteStartStr)
If k = 1 Then 'Site#:
i = i + 1
.Site(i).SiteS = j '开始行数
.Site(i).Site = Trim(Mid(.data(j), k + Len(SiteStartStr))) '编号
.Site(i).SiteNew = .Site(i).Site '新编号,为修改做准备
End If
k = 0 '因为K下面是从判断中来,所以每次都必须清掉
k1 = InStr(1, .data(j), SiteBinstr1) '按第一个条件测试
k2 = InStr(1, .data(j), SiteBinstr2) '按第二个条件测试
If k1 = 1 Then k = k1 '如果有一个等于1 ,则找到
If k2 = 1 Then k = k2
If k = 1 Then
.Site(i).SiteBinS = j '所处行数
'解析SiteBin
.Site(i).SiteBin = Val(Mid(.data(j), k + Len(SiteBinstr1))) '这二个字符串是等长,所以取长度时可以通用,否则需要拆分写
'分析PF
If .Site(i).SiteBin = 1 Then
.Site(i).PF = PASS
.PassCount = .PassCount + 1
Else
.Site(i).PF = FAIL
.FailCount = .FailCount + 1
End If
End If
Next j
End With
Call Form1.显示日志("文件解析完成")
End Sub
只是初步搞了一下,没去测试,保存之类的都没去弄,这个函数用掉了1个小时。
显示部分完全没去弄。