Delete last line from a text file

Is there a quick an easy way to delete the last line of a varying size text file leaving no blanks? The only way I can think of is to copy everything except the last line into another text file and rename it to the original filename.
Who is Participating?
Arthur_WoodConnect With a Mentor Commented:
Essentially, think of the text file like a Cassette tape.  You want to erase the last song on the tape.  The only way to get to the LAST song, is to read EVERY song on the tape, and then copy all but the LAST one that you read.  And you won't know which one is the LAST one until you have read ALL of them.

How can you know if a a specific line of the file is the LAST one, until you have read ALL of them.  Whenever you read a line, there could always be at least one more, until you actually reach the End-Of-File, and only then do you KNOW FOR A FACT that you have actully read the last line of the file.

yes, that is the only way.


Private Sub Command1_Click()
Dim ff As Integer, Filename As String, s As String
Dim Arr

Filename = "your full path file name"
ff = FreeFile
Open Filename For Binary Access Read Lock Read Write As #ff
s = Space$(LOF(ff))
Get #ff, , s
Close ff
Arr = Split(s, vbCrLf)
ReDim Preserve Arr(UBound(Arr) - 1)

ff = FreeFile
Open Filename For Output Access Write As #ff
Print #ff, Join(Arr, vbCrLf)
Close ff

End Sub
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

wildarmsdaveAuthor Commented:
EDDYKT, your code gives a subscript out of range error on ReDim Preserve Arr(UBound(Arr) - 1)

Arthur, thanks for clearing that up. I'm pretty clear on that now.
there is no error checking on the code and you want want to add some.

just do it on the top of the head
try out this

Dim i As Integer
Dim j() As String
i = 0
Dim str

ReDim j(Len(Text1.Text))
j() = Split(Text1.Text, vbCrLf)
While j(i) <> ""
i = i + 1
Text1.Text = ""
For x = 0 To i - 2
Text1.Text = Trim(Text1.Text) & vbCrLf & j(x)
wildarmsdaveAuthor Commented:
Thanks EDDYKT however I have just tried this on a text file containing.

I put no error checking in. The program ran withouot error  but nothing happened to the text file.

koolnurd's code errors on While j(i) <> "" with a subscript error or just seems to get stuck in a loop.
i think you have Vbcrlf on the last line after f

the code only take the last line out including the blank line.

If may want to check it before doing redim


change this line

ReDim Preserve Arr(UBound(Arr) - 1)


Dim i As Long, size As Long

For i = UBound(Arr) + 1 To 1 Step -1
    If Trim(Arr(i - 1)) = "" Then
        size = size + 1
        Exit For
    End If
ReDim Preserve Arr(UBound(Arr) - size)

wildarmsdaveAuthor Commented:
Thanks again EDDYKT but I tried your soloution with and without a VBCRLf in the text file but still nothing happens, the file is exactly the same as it was before?????
If you know the location of the end of the next-to-last line, you can shrink the file size without having to output a new line.

If you go the simpler route of reading and writing the file's contents, I would recommend using a byte array, rather than a string variable.  You'll save memory and extra processing.
wildarmsdaveAuthor Commented:
EDDYKT, I've tried your first set of code again with these differences...

1) Have removed the vbCrlf from the text file
2) Have changed the output file to another location.

This works but once only as it leaves a blank line (maybe a vbcrlf) behind

I tried your 'second' piece of code with these changes

1) I left in the vbcrlf
2) Have changed the output file to another location.

This creates a new file but it's totally blank.

wildarmsdaveAuthor Commented:
I do not need to delete the last line of the text file now as I can ged rid of the 'bad line' directly from the export.

  I am accepting Arthur answer as it provided a clear insight into what I needed to do. EDDYKT's code didn't work for me this time. If it had I would have split the points.

As usual thanks to everyone that replied. Your help and comments are always appreciated.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.