VB单文件txt文件数据提取处理,如何改为批量txt文件数据提取处理呢
以下VB代码实现的是提取当前目录下的Table.txt数据,然后创建Format.txt文件,按特定格式提取数据保存到里面。想要改造为自动扫描当前目录下的.txt文件,然后按扫描到的txt文件名创建_Format.txt后缀,然后按特定格式提取数据保存到里面。
比如:当前目录下有123Table.txt,adadf.txt,iiiiii.txt,运行exe后按特定格式提取数据,然后保存为123123Table_Format.txt,adadf_Format.txt,iiiiii_Format.txt。
Private Sub Click()
FileName = App.Path & "\" & "Table.txt"
If Dir(FileName) <> "" Then
Open FileName For Binary As #1
AllData$ = Input$(LOF(1), 1)
Close #1
arr = Split(AllData$, vbCrLf)
FileNameNew = Replace(LCase(FileName), ".txt", "Format.txt")
Dim sr1 As String
Dim sr2 As String
Dim sr3 As String
Open FileNameNew For Output As #1
For X = 0 To UBound(arr)
If InStr(arr(X), "Register_Table") > 0 Then
Print #1, "//" & arr(X)
Print #1, "const REG_TAB m_reg_tab[]={"
k = 0
ElseIf Left(arr(X), 3) = "Reg" Then
k = k + 1
sr1 = Mid(arr(X), 5, 2)
sr2 = Mid(arr(X), 10, 2)
sr3 = Mid(arr(X), 14, 99)
Print #1, " {0x" & sr1 & ",0x" & sr2 & "},//" & sr3
ElseIf InStr(arr(X), "Ram_Table") > 0 Then
k = 0
Print #1, "//" & arr(X)
Print #1, "const RAM1_TAB m_ram1_tab[]={"
ElseIf Left(arr(X), 4) = "Coef" Then
k = k + 1
sr1 = Mid(arr(X), 6, 2)
sr2 = Mid(arr(X), 11, 6)
sr3 = Mid(arr(X), 18, 99)
Print #1, " {0x" & sr1 & ",0x" & sr2 & "},//" & sr3
ElseIf arr(X) = "" And k > 0 Then
k = 0
Print #1, "};"
End If
Next
Close #1
End If
End Sub