Hi,

I have written a program which reads the text from the text file cleans it for some specific letters or phrases and then saves it in another text file. I am having a problem in rewriting the processed or cleaned data (after it has removed some phrases and characters)onto writing the file.

I have successfully written the text on the file except one problem. In each loop when each string is cleaned and is written on the text file, it overwrites the previous string. What I want is that it should write in the next line or in the same line.

My code is given below:

Private Sub btnopen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnopen.Click

        FileOpen.Title = "Please select the file"
        FileOpen.InitialDirectory = "D:\"
        FileOpen.ShowDialog()

        Dim strm As System.IO.Stream
        strm = FileOpen.OpenFile

        Dim reader As New System.IO.StreamReader(strm)
        'Dim FileName As String = "D:\tempfile.txt"
        Dim readline As String
        Do While reader.Peek() <> -1
            readline = readline & reader.ReadLine & vbNewLine

            If readline.Contains("BAD") = True Then
                readline = Nothing
            Else
                'Starting cleaning of ": FCS OK" at the end of the string
                Dim removecharacter As String = ": FCS OK"
                Dim cleanstring As String = Replace(readline, removecharacter, "")

                'Writing the cleaned up data to the user mentioned text file
                Dim objwriter As New System.IO.StreamWriter("D:\" & FileName.Text & ".txt", False)

                objwriter.WriteLine(cleanstring)
                objwriter.Close()
                readline = Nothing
            End If
        Loop
        'TextBox1.Text = readline
        If Not (strm Is Nothing) Then
            strm.Close()
        End If

    End Sub

Can anyone please tell me that how can I write the text in the new line and avoid overwriting of text in my text file.

Regards,

Bilal A. Khan

I have resolved it... Instead of False in the following code:

Dim objwriter As New System.IO.StreamWriter("D:\" & FileName.Text & ".txt", False)

write True and you will not overwrite the text written in the file

Dim objwriter As New System.IO.StreamWriter("D:\" & FileName.Text & ".txt", True)
commented: for being a dope.a.ss lil' forum member and posting a solution before marking thread as solved. Hope it helps.:) +12

.even If thread.solved, hope this helps.

Imports System.IO
Public Class Form1
    Private myCoolFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\test.txt"
    '// save.
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        File.WriteAllText(myCoolFile, "your content here")
    End Sub
End Class

For every type of saving, I never bothered much w/the: StreamWriter and started using: WriteAllText .
Even when it comes to Appending.lines to a File, I load the file, add my.content Then add the previous File's content, save the file with the simple code I just posted, and it Appends to the top of the File, Not bottom.

Regarding saving more than one String to a File, using the save.code, I use a simple String or 2 to help me along the way.

Imports System.IO
Public Class Form1
    Private myCoolFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\test.txt"
    Private sTCoolString As String, xnL As String = vbNewLine
    '// save.
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        sTCoolString = "" '// .clear for new input.
        For i As Integer = 0 To 9 '// For testing saving lines to file.
            If Not sTCoolString = "" Then sTCoolString &= xnL & i Else sTCoolString = i
        Next
        sTCoolString &= xnL & "done.saving"
        File.WriteAllText(myCoolFile, sTCoolString)
    End Sub
End Class

This is quite simple, it does not require any True/False, and it returns the same results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.