功能
Public Function GetLines(fpath$) As Variant
'REFERENCES:
'Microsoft Scripting Runtime // Scripting.FileSystemObject
'Microsoft VBScript Regular Expressions 5.5 // VBScript_RegExp_55.RegExp
Dim fso As New Scripting.FileSystemObject, RE As New VBScript_RegExp_55.RegExp
If fso.FileExists(fpath) = True Then
Dim mts As MatchCollection, mt As Match
Dim lines() As String
Dim content$: content = fso.OpenTextFile(fpath).ReadAll()
With RE
.Global = True
.Pattern = "[^\r\n]+" 'catch all characters except NewLines/Carraige Returns
If .test(content) = True Then
Set mts = .Execute(content)
ReDim lines(mts.Count - 1)
Dim pos&
For Each mt In mts
lines(pos) = mt.Value
pos = pos + 1
Next mt
Else
MsgBox "'" & Dir(fpath) & "' contains zero bytes!", vbExclamation
End If
End With
GetLines = lines
Else
MsgBox "File not found at:" & vbCrLf & Dir(fpath), vbCritical
End If
End Function
并且可以由 (from immediate window
)调用
?GetLines("C:\BOOT.INI")(2)
和输出
default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
上面的示例可用于从源自任何操作系统的任何文本文件中获取所有行。
希望这可以帮助。