Delete the last 10 characters of a text file vb.net

I don't want to load the full file in memory and delete it, this would be to much overhead, I want the writer to go to the last few characters and delete it or overwrite it with empty charch's..
Mr_EziAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alex_WCommented:
Try StreamReader.ReadBlock() - you can specify a count or characters to read.
0
Mr_EziAuthor Commented:
I want to write not read, also I want the last few characters not the first one's
0
UnifiedISCommented:
You have to read before you can write.  What makes you think that there will be too much overhead?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Mr_EziAuthor Commented:
I don't have a problem the read before write but I don't want to write the full file again.
0
UnifiedISCommented:
What is wrong with writing the entire file?
0
Mr_EziAuthor Commented:
This take me out of memory.
0
UnifiedISCommented:
How big are the text files?
Can you post your code?

0
UnifiedISCommented:
Using this code, I created a text file with 1 million and 1 lines.
Then I read it and wrote all but the last 10 characters to a new file.  I couldn't see any memory usage spikes in task manager.

 Private Sub btnTextFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTextFile.Click
        Dim sPath As String = "C:\BigFile.txt"
        Dim sw As New IO.StreamWriter(sPath)
        Dim sLine As String = "All work and no play makes Jack a dull boy"
        For x As Integer = 0 To 1000000
            sw.WriteLine(sLine)
        Next
        sw.WriteLine("This is the last line")
        sw.Flush()
        sw.Close()

    End Sub

    Private Sub btnReadAndRewrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadAndRewrite.Click
        Dim sPath As String = "C:\BigFile.txt"
        Dim sNewPath As String = "C:\BigFile2.txt"
        Dim sr As New IO.StreamReader(sPath)
        Dim sb As New System.Text.StringBuilder
        While sr.Peek > 0
            sb.Append(sr.ReadLine)
            sb.Append(vbNewLine)
        End While
        sr.Close()
        Dim sw As New IO.StreamWriter(sNewPath)
        sw.WriteLine(sb.ToString.Substring(0, sb.Length - 11))
        sw.Flush()
        sw.Close()
    End Sub

Open in new window

0
Mr_EziAuthor Commented:
Thanks for your code,

There is no way I could just delete this few lines?
0
UnifiedISCommented:
I don't know of a way.  
I'm still curious what it is about your situation that makes memory an issue with reading/writing text files.  Are you sure your code doesn't have a flaw that is causing a memory issue?  
0
meraydin2Commented:
Here you go... Change 10 to something less than file size...

        Dim fs As FileStream = File.OpenWrite("c:\1234.txt")
        fs.SetLength(fs.Length - 10)
        fs.Close()
0
meraydin2Commented:
Also, to overwrite last 10 bytes with space:

        Dim fs As FileStream = File.OpenWrite("c:\1234.txt")
        fs.Seek(fs.Length - 10, SeekOrigin.Begin)

        Dim Space() As Byte = System.Text.Encoding.ASCII.GetBytes(New String(" ", 10))

        fs.Write(Space, 0, Space.Length)
        fs.Close()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nffvrxqgrcfqvvcCommented:
You may also try something like this...
 

Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32.SafeHandles
 
   <DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True, SetLastError:=True)> _
    Private Shared Function SetEndOfFile(ByVal hFile As SafeFileHandle) As Integer
    End Function

    Sub DoStuff()
        Dim fs As New FileStream("j:\dd_NET_Framework20_Setup11F7.txt", FileMode.Open, FileAccess.Write, FileShare.None)
        fs.Seek((-20), SeekOrigin.End)
        SetEndOfFile(fs.SafeFileHandle)
        fs.Flush()
        fs.Close()
    End Sub

Open in new window

0
nffvrxqgrcfqvvcCommented:
Nice meraydin2,
I have never seen the SetLength... Looks like it might be calling SetEndOfFile under the hood but its easier to use SetLength :)
0
Mr_EziAuthor Commented:
Thanks so much!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.