[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

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.
0
wildarmsdave
Asked:
wildarmsdave
  • 5
  • 3
  • 2
  • +2
1 Solution
 
Arthur_WoodCommented:
yes, that is the only way.

AW
0
 
Arthur_WoodCommented:
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.

AW
0
 
EDDYKTCommented:
i.e.

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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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.
0
 
EDDYKTCommented:
there is no error checking on the code and you want want to add some.

just do it on the top of the head
0
 
koolnurdCommented:
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
Wend
Text1.Text = ""
For x = 0 To i - 2
Text1.Text = Trim(Text1.Text) & vbCrLf & j(x)
Next
0
 
wildarmsdaveAuthor Commented:
Thanks EDDYKT however I have just tried this on a text file containing.
a
b
c
d
e
f

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.
0
 
EDDYKTCommented:
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


i.e

change this line

ReDim Preserve Arr(UBound(Arr) - 1)

to

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
    Else
        Exit For
    End If
Next
ReDim Preserve Arr(UBound(Arr) - size)


0
 
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?????
0
 
aikimarkCommented:
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.
Example:
http://www.devx.com/vb2themax/Tip/19309

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.
0
 
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.

0
 
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now