#1
  1. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Mar 2004
    Posts
    57
    Rep Power
    15

    Display Last 3 Lines From Text File


    !Please help me somebody !

    How can I display the last 3
    lines from a text file using ASP.
  2. #2
  3. No Profile Picture
    Grumpier Old Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2003
    Posts
    10,143
    Rep Power
    191
    One way that comes to mind is to set up a 3 node array.

    Make a function that "pushes" each line you read into the array, so when you run across the EOF your array will hold the last 3 lines.
    ======
    Doug G
    ======
    I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain
  4. #3
  5. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Mar 2004
    Posts
    57
    Rep Power
    15

    Red face Please help me with the code


    Originally Posted by Doug G
    One way that comes to mind is to set up a 3 node array.

    Make a function that "pushes" each line you read into the array, so when you run across the EOF your array will hold the last 3 lines.
    Thanks Doug, for the suggestion, but can you please help me with codes of the same.
    Thanks again
  6. #4
  7. No Profile Picture
    Grumpier Old Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2003
    Posts
    10,143
    Rep Power
    191
    Assuming you have your text file opened as myfile, something like

    Code:
    <%
    Dim strArray(2)
    strArray(0) = ""
    strArray(1) = ""
    strArray(2) = ""
    ..
    ..
    Do While not myfile.eof  'Check this syntax
      str1 = myfile.readline
      push str1
    Loop
    myfile.close  'Check this syntax too
    
    lastline = strArray(0)
    nexttolastline = strArray(1)
    thirdfromlastline = strArray(2)
    
    ' Do whatever else you need to do
    
    Sub push(str)
      strArray(2) = strArray(1)
      strArray(1) = strArray(0)
      strArray(0) = str
    End Sub
    %>
    Standard Disclaimer: I haven't run or tested this code you probably need to make adjustments.
    Last edited by Doug G; March 24th, 2004 at 09:53 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Mar 2004
    Posts
    57
    Rep Power
    15
    Hi Doug! Thanks a bunch for ur help, it did worked



    I want one more help if u can. But first I should show the output from this program,



    '-------------------------------------------------------------------------------------

    Set myRead = myFSO.OpenTextFile(Server.MapPath("correction.txt" ), ForReading, True)



    Dim strArray(2)
    strArray(0) = ""
    strArray(1) = ""
    strArray(2) = ""




    Do While not myRead.AtEndOfStream

    str1 = myRead.readline
    push str1
    Loop
    myRead.close


    lastline = strArray(0)
    nexttolastline = strArray(1)
    thirdfromlastline = strArray(2)



    Response.Write "<b>A</b>: " & " " &"<input type=" &""""&"text"&""""&" "& "name=" &""""&"rdate"&""""&" "& "value=" &""""& lastline &""""&">" & " "
    Response.Write "<b>B</b>: " & " " &"<input type=" &""""&"text"&""""&" "& "name=" &""""&"rtitle"&""""&" "& "value=" &""""& nexttolastline &""""&">" & " "
    Response.Write "<b>C</b>: " & " " &"<input type=" &""""&"text"&""""&" "& "name=" &""""&"rmessage"&""""&" "& "value=" &""""& thirdfromlastline &""""&">" & " "
    Response.Write "<br>"


    Sub push(str)
    strArray(2) = strArray(1)
    strArray(1) = strArray(0)
    strArray(0) = str
    End Sub




    ---------------------------------------------------------------------------------

    The OUTPUT is:

    A: B: C:
    ---------------------------------------------------------------------------------


    But can I get the output in the following way:
    A:
    B:
    C:

    So that I can edit the data and append it to the same text file.



    Please help me with this too, Thanks very much

    -------------------------------------------------------------------------------

  10. #6
  11. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Mar 2004
    Posts
    57
    Rep Power
    15

    It worked...please see the attchd file


    Sorry for last message, I wanted to send the output in textfield but couldnt do that. SO now sending
    an attachment in word file.

    ------------------------------------------------------------------------

    Originally Posted by Doug G
    Assuming you have your text file opened as myfile, something like

    Code:
    <%
    Dim strArray(2)
    strArray(0) = ""
    strArray(1) = ""
    strArray(2) = ""
    ..
    ..
    Do While not myfile.eof 'Check this syntax
    str1 = myfile.readline
    push str1
    Loop
    myfile.close 'Check this syntax too
     
    lastline = strArray(0)
    nexttolastline = strArray(1)
    thirdfromlastline = strArray(2)
     
    ' Do whatever else you need to do
     
    Sub push(str)
    strArray(2) = strArray(1)
    strArray(1) = strArray(0)
    strArray(0) = str
    End Sub
    %>
    Standard Disclaimer: I haven't run or tested this code you probably need to make adjustments.
    Attached Files
  12. #7
  13. No Profile Picture
    Grumpier Old Moderator
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2003
    Posts
    10,143
    Rep Power
    191
    I haven't looked at your file, but -

    If you are reconstructing the lines you should be able to use the writeline method.

    If you want to display the lines in a text file with line breaks, just add a vbcrlf at the end of the string, like

    strArray(0) = strArray(0) & vbcrlf

    If you want to display properly in a browser, you need to include <br /> at the end of the strings.
  14. #8
  15. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Mar 2004
    Posts
    57
    Rep Power
    15

    Thanks for ur help


    Thanks Doug for your help. Quoting the program for others to get reference:

    '------------------------------------------------------------------------
    Set myRead = myFSO.OpenTextFile(Server.MapPath("correct.txt"), ForReading, True)

    Dim strArray(2)
    strArray(0) = ""
    strArray(1) = ""
    strArray(2) = ""

    Do While not myRead.AtEndOfStream
    str1 = myRead.readline
    push str1
    Loop

    myRead.close

    lastline1 = strArray(0)
    lastline = Split( lastline1, "|" )

    nexttolastline1 = strArray(1)
    nexttolastline = Split( nexttolastline1, "|" )
    thirdfromlastline1 = strArray(2)
    thirdfromlastline = Split( thirdfromlastline1, "|" )
    '------------------------------------------------------------
    '------------------------------------------------------------
    Response.Write "<b>1</b>: " & " " &"<input type=" &""""&"text"&""""&" "& "size=" & """" & "40" & """" & " " &"name=" &""""&"rdate1"&""""&" "& "value=" &""""& thirdfromlastline(0) &""""&">" & " "
    Response.Write "<br>"
    Response.Write "<input type=" &""""&"text"&""""&" "& "size=" & """" & "40" & """" & " " &"name=" &""""&"rtitle1"&""""&" "& "value=" &""""& thirdfromlastline(1) &""""&">" & " "
    Response.Write "<br>"
    Response.Write "<TEXTAREA cols=" & """" & "45" & """" & " " &"name=" &""""&"rmessage1"&""""&">"& thirdfromlastline(2) & "</TEXTAREA>" & " "
    Response.Write "<br>"
    Response.Write "<b>2</b>: " & " " &"<input type=" &""""&"text"&""""&" "& "size=" & """" & "40" & """" & " " &"name=" &""""&"rdate2"&""""&" "& "value=" &""""& nexttolastline(0) &""""&">" & " "
    Response.Write "<br>"
    Response.Write "<input type=" &""""&"text"&""""&" "& "size=" & """" & "40" & """" & " " &"name=" &""""&"rtitle2"&""""&" "& "value=" &""""& nexttolastline(1) &""""&">" & " "
    Response.Write "<br>"
    Response.Write "<TEXTAREA cols=" & """" & "45" & """" & " " &"name=" &""""&"rmessage2"&""""&">"& nexttolastline(2) & "</TEXTAREA>" & " "
    Response.Write "<br>"
    Response.Write "<b>3</b>: " & " " &"<input type=" &""""&"text"&""""&" "& "size=" & """" & "40" & """" & " " &"name=" &""""&"rdate3"&""""&" "& "value=" &""""& lastline(0) &""""&">" & " "
    Response.Write "<br>"
    Response.Write "<input type=" &""""&"text"&""""&" "& "size=" & """" & "40" & """" & " " &"name=" &""""&"rtitle3"&""""&" "& "value=" &""""& lastline(1) &""""&">" & " "
    Response.Write "<br>"
    Response.Write "<TEXTAREA cols=" & """" & "45" & """" & " " &"name=" &""""&"rmessage3"&""""&">"& lastline(2) & "</TEXTAREA>" & " "
    Response.Write "<br>"
    Response.Write "<hr>"
    'Response.Write "</center>"

    Sub push(str)
    strArray(2) = strArray(1)
    strArray(1) = strArray(0)
    strArray(0) = str
    End Sub
    '----------------------------------------------------------------------------------



    Originally Posted by Doug G
    I haven't looked at your file, but -

    If you are reconstructing the lines you should be able to use the writeline method.

    If you want to display the lines in a text file with line breaks, just add a vbcrlf at the end of the string, like

    strArray(0) = strArray(0) & vbcrlf

    If you want to display properly in a browser, you need to include <br /> at the end of the strings.

Similar Threads

  1. reading lines in text file
    By tmangin in forum Visual Basic Programming
    Replies: 8
    Last Post: April 20th, 2005, 12:27 PM
  2. Display the last 3 lines from my text file using ASP
    By yasuru in forum ASP Development
    Replies: 0
    Last Post: March 23rd, 2004, 11:37 PM
  3. Web serverístatistical analysis
    By cso in forum ASP Development
    Replies: 0
    Last Post: April 19th, 2003, 11:35 AM
  4. Reading a text file
    By Steve Schofield in forum ASP Development
    Replies: 1
    Last Post: February 2nd, 2002, 01:09 AM
  5. Display content of text file
    By Steve Schofield in forum ASP Development
    Replies: 2
    Last Post: June 14th, 2000, 01:55 AM

IMN logo majestic logo threadwatch logo seochat tools logo