Avatar of fwsteal
fwsteal

asked on 

seek to end of file, delete line

I need help with seeking to the end of a file, deleting a line and saving the file. Below is my windows text file and my code so far.

Windows Text File

102,,agent_disconnect,2007-02-27 22:54:44,0xffffffff,0xffffffff,,,,000.184.00.131,000.184.00.131,,,,,,,,,,,,
102,,agent_disconnect,2007-02-27 22:54:44,0xffffffff,0xffffffff,,,,00.109.000.010,00.109.000.010,,,,,,,,,,,,
102,,agent_discon
------------------------
Code:

//Goal: read to end of file and delete the last line, then save the file as win.txt
Dim myFile As String
myFile = "c:\logfiles\winlog.txt"

Dim fs As New FileStream("myFile", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
fs.Seek(0, SeekOrigin.End)
'read back and remove the last line?
'now save as new file in c:\logfiles\temp\win.txt
fs.Close()
Visual Basic.NET

Avatar of undefined
Last Comment
Mike Tomlinson
ASKER CERTIFIED SOLUTION
Avatar of tpwells
tpwells

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Hey Fernando...instead of rewriting the whole file, try using SetLength() to truncate the existing stream you have:
http://msdn2.microsoft.com/en-us/library/system.io.filestream.setlength.aspx

    "If the given value is less than the current length of the stream, the stream is truncated."

I haven't tied it myself...but maybe:

    fs.SetLength(currentPosition)
    fs.Close()
Hi Idle_Mind;

You are correct of course but in this case fwsteal wanted to save it to a different file.

You have a great day. ;=)

Fernando
Hehe...good point Fernando!

My short term memory even amazes me sometimes...  =)
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo