程序代码:
Option Explicit
Public Const DeviceStr = "Device#:"
Public Const DeviceStrTitle = " Number Site Result Test Name Pin Channel Low Measured High Force Loc"
Public Const SiteBin = " Site Sort Bin"
Public Const Sitetests = " Site Failed tests/Executed tests"
Public Const Device_F = "========================================================================="
Public Const site_F = "------------------------------------"
Public Enum PassFaiL
PASS = 1
FAIL = 2
End Enum
Public Enum 清显示范围
全部 = 0
分析 = 1
删除 = 2
End Enum
Public Type DeviceType
Device As String 'Device
DeviceS As Long '所处行数
Sitetests As Long
SitetestsS As Long
SiteBin As Long
SiteBinS As Long
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
data() As String '文件数据
Datacount As Long '总行数
'文件头,具体名字,参考 FileT 数组定义
FileT(9) As String
'数据组数
Count As Long
'数据
Device() As DeviceType
PASS As Long '统计
FAIL As Long
End Type
Public PF As FileTYpe
Public FileT(9) As String
Public Sub 文件解析() '不传参数,填充全局变量 PF
'不负责备份文件
Dim i As Long, j As Long
Dim k As Long, tmp1 As String
Dim fr As Long
fr = FreeFile
With PF
.Datacount = 0
.Count = 0
.FAIL = 0
.PASS = 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)
tmp1 = "" '清掉内存
.Count = 0
Call Form1.显示日志("正在分析步骤一..")
For j = 0 To .Datacount
If j Mod 10 = 0 Then '每处理10个变化一次进度条
Call Form1.进度条(0.15 + j / .Datacount * 0.43)
End If
For i = 0 To 8
k = InStr(1, .data(j), FileT(i))
If k > 0 Then
tmp1 = Trim(Mid(.data(j), k + Len(FileT(i)) + 1))
.FileT(i) = tmp1
Exit For '本次循环处理完毕
End If
Next i
If InStr(1, .data(j), FileT(9)) > 0 Then 'Site Number 数据在下一行
tmp1 = Trim(.data(j + 1))
.FileT(9) = tmp1
End If
If InStr(1, .data(j), DeviceStr) > 0 Then 'Device#:
.Count = .Count + 1
End If
Next j
Call Form1.显示日志("正在分析步骤二..")
i = 0
ReDim .Device(.Count)
For j = 0 To .Datacount
If j Mod 10 = 0 Then '每处理10个变化一次进度条
Call Form1.进度条(0.58 + j / .Datacount * 0.4)
End If
k = InStr(1, .data(j), DeviceStr)
If k > 0 Then 'Device#:
i = i + 1
.Device(i).DeviceS = j
.Device(i).Device = Trim(Mid(.data(j), k + Len(DeviceStr)))
End If
k = InStr(1, .data(j), SiteBin)
If k > 0 Then
.Device(i).SiteBinS = j + 2
'解析SiteBin
.Device(i).SiteBin = Mid(.data(j + 2), 24, 8)
End If
k = InStr(1, .data(j), Sitetests)
If k > 0 Then
.Device(i).SitetestsS = j + 2
'解析Sitetest
.Device(i).SiteBin = Val(Mid(.data(j + 2), 14, 8))
End If
Next j
Call Form1.显示日志("正在分析步骤三..")
'解析完需要统计
For j = 1 To .Count
If j Mod 10 = 0 Then '每处理10个变化一次进度条
Call Form1.进度条(0.98 + j / .Count * 0.02)
End If
If .Device(j).SiteBin = 1 And .Device(j).Sitetests = 0 Then
.Device(j).PF = PASS
.PASS = .PASS + 1
Else
.Device(j).PF = FAIL
.FAIL = .FAIL + 1
End If
Next j
End With
Call Form1.显示日志("文件解析完成")
End Sub
Public Sub 保存Pass()
'Dim fn As String
Dim fr As Long
fr = FreeFile
Dim i As Long
Dim j As Long
Call Form1.进度条(0.01, "开始保存Pass数据")
Open (PF.Path & "\" & "Tmp-Pass.txt") For Output As fr
Call Form1.进度条(0.01, "写入文件头")
'先写入头
For j = 0 To PF.Device(1).DeviceS - 1
Print #fr, PF.data(j)
Next j
Call Form1.进度条(0.05, "开始写入数据")
'再写入每节
For i = 1 To PF.Count
Call Form1.进度条(0.05 + i / PF.Count * 0.9)
If PF.Device(i).PF = PASS Then
For j = PF.Device(i).DeviceS To PF.Device(i).SiteBinS
Print #fr, PF.data(j)
Next j
'写完后,写入分隔符
Print #fr, Device_F
End If
Next i
Close #fr
If Dir(PF.PathName & "-Pass.txt") <> "" Then
Kill (PF.PathName & "-Pass.txt")
End If
Name (PF.Path & "\" & "Tmp-Pass.txt") As (PF.PathName & "-Pass.txt")
Call Form1.进度条(1, "保存数据完成!")
End Sub