这是一个可用于加载文件内容的函数:
Public Function LoadFile(dFile As String) As String
Dim ff As Integer
On Error Resume Next
ff = FreeFile
Open dFile For Binary As #ff
LoadFile = Space(LOF(ff))
Get #ff, , LoadFile
Close #ff
End Function
接下来,您想要拆分该文件的输出。
首先,您需要知道后端将生成什么类型的 EOL 终止字符。假设每行以回车符 (13) 和换行符 (10) 结尾,您可以使用以下代码将每行存储到字符串数组中:
Dim lines() As String
lines = Split(LoadFile("LANrealm.log"), vbCrLf)
最后,需要循环遍历每一行(使用 For...Next 循环)并查找您想要提取的任何信息:
For i = 0 To Ubound(lines)
' Add here necessary logic to extract the information.
' Each line can be accessed by indexing the array as: lines(i)
Next
希望这可以帮助您开始...
测试代码:
- 启动 VB6 并创建一个新项目。 VB6 将创建一个具有一种窗体的空项目
- 双击表格即可查看
- 右键单击工具箱并选择“组件”
- 找到“Microsoft 通用对话框控件”并选择它
- Click OK
- 现在,将“CommonDialog”组件从工具箱拖到窗体上
- 双击表单查看其源代码
- 粘贴以下代码
注意:确保覆盖任何预先存在的代码
Option Explicit
Private Sub Form_Load()
cDlg.DefaultExt = "txt"
cDlg.Filter = "Text Files|*.txt;*.log"
cDlg.ShowOpen
If cDlg.fileName <> "" Then AnalyzeFile .fileName
End Sub
Private Sub AnalyzeFile(fileName As String)
Dim fileContents As String
Dim lines() As String
Dim i As Integer
fileContents = LoadFile(fileName)
lines = Split(fileContents, vbCrLf)
For i = 0 To UBound(lines)
If InStr(1, lines(i), "event:", vbTextCompare) Then
MsgBox "Line #" & i & " contains the string 'event'" + vbCrLf + vbCrLf + lines(i)
End If
Next
End Sub
Private Function LoadFile(dFile As String) As String
Dim ff As Integer
On Error Resume Next
ff = FreeFile
Open dFile For Binary As #ff
LoadFile = Space(LOF(ff))
Get #ff, , LoadFile
Close #ff
End Function
运行该程序,当要求提供文件时,选择后端将生成的日志之一。
在这个例子中,程序会告诉你哪些行包含“事件信息”,例如“事件:Roper杀了Pleb(M4A1)射击=5英尺=2身体=2头=1”。