CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Files

  1. #1
    Join Date
    May 2001
    Posts
    25

    Files

    Hello,
    Is there any function in vb that can bring me the last line of a text file?
    Right now i read line by line. It's ok for little file but not for big file.

    tnx

    Go scuba diving...

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Files

    There is not that kind of function as far as I know, but
    John "The King" G. Duffy showed the fastest way to read a file
    http://codeguru.com/cgi-bin/bbs/wt/s...age=&view=&sb=

    if file is really big, this may not work fine,owever it is worth a try

    You can read the whole file in a variable and then strip last line from
    variable (which is the line that from back to top starts with a carriagereturn)
    So you can use the "instrRev" function to find first vbcrlf in the big var
    resulting from that kind of reading and use the right$ function to
    extraxt the line you're looking for.
    Hope this may help

    Cesare Imperiali

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Files

    Actually there is a better way to find the last line in a file but it still involves reading the file. Here is a sample using a RichTextBox.
    The primary purpose of this sample was for a Row/Column counter. but is can be used for anything. the COmmand1 routine will load a fiel then msgbox the last line
    For the sample, Add a Richtextbox (RTF) two text boxes txtRow, TxtCol, and a command button (Command1). Paste this code click the command button

    private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, byval lParam as Any) as Long
    Const EM_LINEINDEX = &HBB ' get offset of current line
    '------------ Called any time that the I
    ' -Beam moves on the screen -------------


    private Sub Command1_Click()
    Dim X
    rtf.LoadFile "C:\Autoexec.Bat"
    rtf.SelStart = len(rtf.Text) - 1
    rtf.Refresh
    X = rtf.SelStart - SendMessage(rtf.hwnd, EM_LINEINDEX, -1, 0&) + 1
    ' display last line of text file
    MsgBox mid(rtf.Text, len(rtf.Text) - X)
    End Sub

    private Sub rtf_SelChange()
    txtRow = rtf.GetLineFromChar(rtf.SelStart) + 1 ' get the current line
    txtcol = rtf.SelStart - SendMessage(rtf.hwnd, EM_LINEINDEX, -1, 0&) + 1
    End Sub




    John G

  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Files

    See my response to this post for a better mousetrap

    John G

  5. #5
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Files

    Very nice, John!

    Iouri Boutchkine
    iouri@hotsheet.com
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Yes, nice!

    Thanks, John "TheKing" G. Duffy




    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Files

    <de-lurk>

    Here's a simple way - it uses the 'Seek' command in VB to go to an assigned distance from the end of the file, then reads in the rest of the file and looks for the 'VBCRLF' :

    Code:
    private Function GetLastLine(byval FileName as string) as string
        Dim iFile as Integer
        Dim lFileLen as Long
        Dim sBuffer as string
        Dim iPos as Integer
    '
    ' You might need to play with the offset depending on how big
    ' your 'lines' usually are in the file
    '
        Const OFFSET as Long = 160
    '
    ' The line delimiter string - in this case I'm using VBCRLF, it
    ' could just be LF in some cases, again, be careful
    '
        Const FINDSTR as string = vbCrLf
    '
        iFile = FreeFile
        sBuffer = Space$(OFFSET)
    '
        Open FileName for binary Access Read as #iFile
        seek #iFile, (LOF(iFile) - OFFSET)
        get #iFile, , sBuffer
        Close #iFile
    '
        iPos = InStrRev(sBuffer, FINDSTR)
        If iPos = 0 then
            GetLastLine = sBuffer
        else
            GetLastLine = mid$(sBuffer, iPos + len(FINDSTR))
        End If
    '
    End Function



    - of course, you might want to put the 'seek' part inside a loop if the line is longer than your OFFSET value - use at your own risk.

    </de-lurk>


    Chris Eastwood
    VBCodeLibrary - http://www.vbcodelibrary.com
    Last edited by Cimperiali; July 22nd, 2004 at 04:54 PM.

  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: You!!!


    You're here, you answer (and a Top one, too), and I am already out of votes!!

    Cruel life!!






    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: You!!!

    <de-lurk>

    You didn't see 'nuffin.

    Move along now.... :wink

    </de-lurk>

    Chris Eastwood
    VBCodeLibrary - http://www.vbcodelibrary.com

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

Featured