VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



FAST Tail Function for VB

by pt (4 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

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.

Assumes
Assumes a publicly declared dynamic array.

Rate FAST Tail Function for VB

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

Download this snippet    Add to My Saved Code

FAST Tail Function for VB Comments

No comments have been posted about FAST Tail Function for VB. Why not be the first to post a comment about FAST Tail Function for VB.

Post your comment

Subject:
Message:
0/1000 characters