Results 1 to 3 of 3

Thread: File Access - Reading the Last Line?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Posts
    158

    File Access - Reading the Last Line?

    This is for Excel97 VBA if it matters.

    I've been trying to read up on the file access methods but I'm a little slow this morning:
    (example) http://www.erlandsendata.no/english/...eaccessgeneral

    OK, I'm reading a lot of Oracle .sql files (text files, sometimes very big that are created from Oracle queries). Some of these files can get up to 33MB or so. I read them into a spreadsheet and then make reports with them.

    Is there a way to quickly read the last line (or last two lines) of the text file? With a 33MB text file it takes a LONG time to INPUT# each line in a loop just to get to the last line.

    I'm just trying to make a preview window on a form to show the first 10 or so lines and then grab the last line (or last two lines) which contain some file info.


    To sum up the question:
    I want to open a txt file and INPUT# the first ten lines, then skip quickly to the last two lines and put those two lines in string variables.


    Thanks

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    I think its possible to use one of the file pointer variables/functions to return the characters at a certain point.

    Then you could loop bakwards until you have the two/three/four return characters (chr$(10) or chr$(13) or both).

    Not actually messed around with text file manipulation inna while .. I'll experiment.

    You'll need the length of the file, which is a function too. Whether or not it'll work with 33mb files.. dunno . worth a try.

    As a test, cut the top 20 lines, display 10 of them and the last two.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    VB Code:
    1. Public Sub scanfile(ByVal strFile As String, ByVal lngBotRows As Long)
    2.  
    3.     Dim strLine As String, strChar As String
    4.     Dim dblLen As Double, dblLoop As Double
    5.     Dim lngFound As Long
    6.     Dim blnLeave As Boolean
    7.    
    8.    
    9.     Close #1
    10.     Open strFile For Binary As #1
    11.    
    12. '    Debug.Print "End of File", EOF(1)
    13. '    Debug.Print "File Length", FileLen(strFile), LOF(1)
    14. '    Debug.Print "Position in file", Loc(1)
    15.    
    16.     Line Input #1, strLine
    17. '    Debug.Print strLine
    18.    
    19.     blnLeave = False
    20.     lngFound = 0
    21.     dblLoop = LOF(1) - 1
    22.     Do Until blnLeave
    23.        
    24.         Seek #1, dblLoop
    25.         strChar = String(2, " ")
    26.         Get #1, , strChar
    27.        
    28.         If strChar = vbCrLf Then lngFound = lngFound + 1
    29.         If lngFound > lngBotRows Then blnLeave = True
    30.         If dblLoop < 1 Then blnLeave = True
    31.         dblLoop = dblLoop - 1
    32.     Loop
    33.    
    34.     For dblLoop = 0 To lngBotRows
    35.         Line Input #1, strLine
    36.         Debug.Print "Line : ", strLine
    37.     Next
    38.    
    39.     Close #1
    40.  
    41. End Sub

    Attached is the test text file.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width