Results 1 to 8 of 8

Thread: how do i get the last line of a file and erase it?

  1. #1
    Guest

    Question

    hi all,
    lets say i have a text file, now i want to check if the last line equals to something.
    if it does then erase it, replace it with a new one and the put it back (even better just add a sentence before it).

    and if it doest exist then add it.

    but i prefer just to add a line before the last line

    thanks..

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <P>

    this will overwrite the line you search for..
    [code]
    'the results are what you want.
    'search a filefor a line of code

    Option Explicit
    Option Compare Text



    Private Sub Form_Load()
    Dim myArr() As Variant
    Dim myVar As String
    Dim i As Integer
    Open "c:\try.txt" For Input As #1
    Do Until EOF(1)
    i = i + 1
    ReDim Preserve myArr(1 To i) As Variant
    Line Input #1, myVar
    myArr(i) = myVar
    myArr(i) = Trim(myArr(i))
    If myArr(i) = "End Select" Then
    myArr(i) = "jump off a cliff"
    End If

    Loop
    Close #1
    Open "c:\try.txt" For Output As #2
    For i = 1 To i
    Write #2, myArr(i)

    Next
    Close


    End Sub
    [\code]



    [Edited by HeSaidJoe on 07-13-2000 at 08:48 AM]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455
    Hello bzemer,

    I don't know what you are meaning but I do think the next source will help you.

    Private Sub Form_Load()
    Dim SourceFile, TargetFile, Row As String
    SourceFile = "c:\Source.Txt"
    TargetFile = "c:\Target.Txt"

    If Dir(TargetFile) <> "" Then Kill (TargetFile)

    Open SourceFile For Input As #1
    Open TargetFile For Append As #2

    While Not EOF(1) 'read all lines of source file
    Line Input #1, Row 'read a row

    Print #2, Row 'write a row
    Wend
    End Sub

    Nice regards,

    Michelle

  4. #4
    Guest

    Exclamation bugs""""""""""""""""

    ok, i used the first code like this:

    Function replace()
    Dim myArr() As Variant
    Dim myVar As String
    Dim i As Integer
    Open "c:\_test.html" For Input As #1
    Do Until EOF(1)
    i = i + 1
    ReDim Preserve myArr(1 To i) As Variant
    Line Input #1, myVar
    myArr(i) = myVar
    myArr(i) = Trim(myArr(i))
    If myArr(i) = "</body></html>" Then
    myArr(i) = ""
    End If

    Loop
    Close #1
    Open "c:\_test.html" For Output As #2
    For i = 1 To i
    Write #2, myArr(i)
    'MsgBox myArr(i)
    Next
    Close

    End Function

    and it does finds </body></html> and removes them.
    and i can build the html like i wanted.
    but..:
    the html gets filled with """"""""" everywere.
    i cant figure why

    here is what i got:

    """<html><body>"""
    """<title></title>"""
    """"""
    """<text='0000ff'><h7>5</h7> <br>"""
    """"""
    "<text='0000ff'><h7>5</h7> <br>"
    ""
    <text='ffffff'><h7>Total: 10
    <br>---------------------

    </h7>
    </body></html>

    please help?

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    That is because the file is saving as a comma delimited text file...

    How to get around it is another question for which
    I don't have the answer...I'll look around incase
    you can't get it here.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6
    Guest

    Smile oh..

    well.. please do so if you can, the chances that i would find the answere my self are pretty low..

    thanks..

  7. #7
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    The problem lays in the choice of the Write Statement. This inserts quotation marks and commas between strings. Use the Print statement to write the file back instead.

    From MSDN
    Unlike the Print # statement, the Write # statement inserts commas between items and quotation marks around strings as they are written to the file. You don't have to put explicit delimiters in the list. Write # inserts a newline character, that is, a carriage return–linefeed (Chr(13) + Chr(10)), after it has written the final character in outputlist to the file.
    Iain, thats with an i by the way!

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946
    yup...same answer as I found.

    Thanks...

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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