UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> LastLines    

Usage:

CODE

Private Sub Command0_Click()
   Dim strPath As String
   Dim strFile As String
   
   strPath = "MyPath"
   strFile = "MyFile.xml"
   
   Debug.Print LastLines(strPath, strFile, 11)
End Sub

Function:

CODE

'-------------------------------------------------------------------------------------------------------------------
' LastLines
' http://www.utteraccess.com/wiki/LastLines
' Code courtesy of UtterAccess Wiki
' Original submission by Diego F.Pereira-Perdomo
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2013-10-27                    Reads the Last Lines of a file in a memory efficient manner.
'                                       strPath: A string that contains the Path where the File is located.
'                                       strFile: A string that contains the Name of the File.
'                                       lngNodes: The Number of Lines to be read.
'-------------------------------------------------------------------------------------------------------------------
Public Function LastLines(strPath As String, _
                          strFile As String, _
                         lngNodes As Long) As String
                     
   Dim oFSO    As Scripting.FileSystemObject
   Dim oTSt    As Scripting.TextStream
   Dim strPF   As String
   Dim strSL   As String
   Dim i       As Long
   Dim j       As Long
     
   strPF = strPath & "\" & strFile
   
   Set oFSO = New Scripting.FileSystemObject
   Set oTSt = oFSO.OpenTextFile(strPF, ForReading)
   
   
   With oTSt
       Do While Not .AtEndOfStream
           .SkipLine
       Loop
       i = .Line
       .Close
   End With
       j = i - lngNodes
       
   Set oTSt = Nothing
   Set oTSt = oFSO.OpenTextFile(strPF, ForReading)
   
   With oTSt
       Do While Not .AtEndOfStream
           Select Case .Line
               Case j To i
                   strSL = strSL & .ReadLine & vbCrLf
               Case Else
                   .SkipLine
           End Select
       Loop
       .Close
   End With
   
   Set oTSt = Nothing
   Set oFSO = Nothing
   
   LastLines = strSL

End Function

Timings:

CODE

Start counting lines:       10/27/2013 7:23:31 PM
End counting lines:         10/27/2013 7:24:54 PM
Number of Lines:             23348786
Calculated Start Line:       23348775
Start getting last lines:   10/27/2013 7:24:55 PM
End getting last lines:     10/27/2013 7:26:26 PM

RAM consumption: 20Mb

Edit Discussion
Custom Search


Thank you for your support!
This page has been accessed 1,340 times.  This page was last modified 00:59, 28 October 2013 by genoma111.   Disclaimers