Removing complete lines of text

darknuke

Well-known member
Joined
Oct 3, 2003
Messages
68
How would I go about removing a complete line of text?

line1
line2
line3
line4

How would I remove line2 in that example?
 
Youll have to

1. open the file. (System.IO.File).
2. use a stream (System.IO.Stream) to search for the string/ browse to the line to delete.
3. Delete the line and save/close the file.

Hopefully this is enough.
 
Mind if I see an example, I couldnt quite figure out how to indentify lines to delete or even how to delete lines using streams, or else I wouldnt have asked.
 
Im not sure if I can work out how to cut and paste in an example... I am an idiot after all.

Heres a good example:

http://www.computerhelp.forum/showthread.php?s=&threadid=73834&highlight=search+a+file

If youre only using small files, Id do this (otherwise look at the more efficent but more complicated method in the link above):
Code:
    Private Sub FixFile()

        Dim objFile As System.IO.File
        Dim objSR As System.IO.StreamReader
        Dim objSW As System.IO.StreamWriter
        Dim strFileContents As String
        Dim strFilePath As String = "C:\Projects\FileOpen\bin\File.txt"
        Dim strStringToFind As String = "Line 3"

        Open the file
        objSR = objFile.OpenText(strFilePath)

        Read the file
        strFileContents = objSR.ReadToEnd
        objSR.Close()

        Replace the string
        strFileContents = strFileContents.Replace(strStringToFind, "")

        Recreate the file
        objSW = objFile.CreateText(strFilePath)

        Write the file back
        objSW.WriteLine(strFileContents)
        objSW.Close()

        MessageBox.Show("All done")

    End Sub
 
>.< Not what I was looking for. I am trying to remove a line no matter what it contains, not searching for a line that contains a search string.


Perhaps a better example:

Hi,
my name
is
Mr.
Bond.

I want to remove line 3 which in this instance contains "is"

Hi,
is
your name
Trent
Easton.

I want to remove line 3 which in this instance contains "your name"

Does that give you a clarified view on what I am trying to do?
 
what about:

Code:
Private Sub FixFile()

        Dim objFile As System.IO.File
        Dim objSR As System.IO.StreamReader
        Dim objSW As System.IO.StreamWriter
        Dim strTheLine As String
        Dim strFilePath As String = "C:\Projects\FileOpen\bin\"
        Dim intLineNum As Integer = 3
        Dim intCurrentLine As Integer

        Open the file
        objSR = objFile.OpenText(strFilePath & "File.txt")

        Recreate the file
        objSW = objFile.CreateText(strFilePath & "Output.txt")

        Read the file
        intCurrentLine = 1
        strTheLine = ""
        Do While Not strTheLine Is Nothing
            strTheLine = objSR.ReadLine
            If intCurrentLine <> intLineNum Then
                objSW.WriteLine(strTheLine)
            End If
            intCurrentLine = intCurrentLine + 1
        Loop
        objSR.Close()
        objSW.Close()

        MessageBox.Show("All done")

    End Sub
 
if you want to remove the line of text ( not make a new textfile with it removed , but keep the existing one ) ...
Code:
        Dim sReader As New IO.StreamReader(New IO.FileStream("C:\remove line.txt", IO.FileMode.Open, IO.FileAccess.ReadWrite))
        Dim line As String
        Dim strResult As String
        While Not sReader.Peek = -1
            line = sReader.ReadLine
            If line.IndexOf("line2") = -1 Then
                strResult += line & Environment.NewLine
            End If
        End While
        sReader.Close()

        Dim sWriter As New IO.StreamWriter("C:\remove line.txt", False)
        Dim bt As Byte() = System.Text.Encoding.ASCII.GetBytes(strResult.ToCharArray)
        sWriter.BaseStream.Write(bt, 0, bt.Length)
        sWriter.Close()
 
smith: Ill have to make it overwrite the old file, but thank you! :)
 
Last edited by a moderator:
Depending on the size of the file it would be possible to read it all into a string or arraylist, and then write it back out to the same file... anything less than a meg would PROBABLY be ok.
 
Code:
  If System.IO.File.Exists(dbToUse.Text) = True Then
            Dim textStreamReader As IO.StreamReader = System.IO.File.OpenText(dbToUse.Text)
            Dim currentLine As String = ""
            Dim theArray As New Collection

            Do While Not currentLine Is Nothing
                currentLine = textStreamReader.ReadLine
                theArray.Add(currentLine)
            Loop
            textStreamReader.Close()
            theArray.Remove(dataBox.SelectedIndex + 1)
            Try
                FileOpen(1, dbToUse.Text, OpenMode.Output, OpenAccess.Write)
                For Each currentLine In theArray
                    PrintLine(1, currentLine)
                Next currentLine
                FileClose(1)
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
            End Try
            loadDB()
        End If

Ah much better! :)
 
Back
Top