Advertisement
ASP_Volume2 Files/ File Controls/ Input/ Output #29150

FAST Tail Function for VB

After hunting for this functionality since I started using VB, I've finally figured out how to *quickly* read the last few lines in a file. This function fills a dynamic array with the last X number of lines in a file you specify. I use it to monitor apachewin32 logs on another server. This is much faster than using 'line input'. I know there may be more optimized ways of doing this, I just thought I should get it out there.

AI

สรุปโดย AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

ซอร์สโค้ด
original-source
Public Function Tail(fName As String, NumOfLines As Integer, ArrayName() As String)
Dim ff As Integer
Dim raw As String
Dim lines() As String 'used to hold the lines of the text file
Dim lStart As Integer 'switch to LONG if you have over 65k lines.
If Not fileExist(fName) Then
MsgBox "File Not Found - " & vbCrLf & fName, vbCritical, "Error"
Exit Function
End If
ff = FreeFile
Open fName For Binary As #ff
raw = String$(LOF(ff), 32)
Get #ff, 1, raw
Close #ff
lines() = Split(raw, vbNewLine) 'this assumes that the data is stored in individual lines.
ReDim ArrayName(NumOfLines)
If NumOfLines > UBound(lines) Then NumOfLines = UBound(lines)
lStart = UBound(lines) - NumOfLines
For i = 1 To NumOfLines
ArrayName(i) = lines(lStart + i)
Next i
End Function
'and the bonus 'FILEEXIST' function:
Public Function fileExist(filename As String) As Boolean
 Dim l As Long
 On Error Resume Next
 
 l = FileLen(filename)
 
 fileExist = Not (Err.Number > 0)
 
 On Error GoTo 0
End Function
ความคิดเห็นดั้งเดิม (3)
กู้คืนจาก Wayback Machine