Results 1 to 3 of 3

Thread: delete the last three lines

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    delete the last three lines

    how can i delete the last three lines from a .txt file
    if they was empty?

  2. #2

  3. #3
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: delete the last three lines

    Possibly another way
    VB Code:
    1. Dim Prts() As String
    2. Dim txtFile As String
    3.  
    4. 'Read the file
    5. Open "D:\test.txt" For Input As #1
    6.     txtFile = Input(FileLen("D:\test.txt"), 1)
    7. Close #1
    8.  
    9. 'Split the text
    10. Prts() = Split(txtFile, vbCrLf)
    11.  
    12. 'If the last three lines are empty then remove them
    13. If Prts(UBound(Prts)) = "" And Prts(UBound(Prts) - 1) = "" And Prts(UBound(Prts) - 2) = "" Then
    14.     ReDim Preserve Prts(UBound(Prts) - 3)
    15. End If
    16.  
    17. 'Re put the parts back together
    18. txtFile = Join(Prts(), vbCrLf)
    19.  
    20. 'Write it back
    21. Open "D:\test.txt" For Output As #1
    22.     Print #1, txtFile
    23. Close #1

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