Ok so i have a uni assignment to delete lines of text out of a text file, so far i have figured out how to search the text file for specific bits of text, i,e name of user, ive also used a loop to find out what line in the text file the specific line exists as...my code so far is

Dim line As String
        Dim Input As StreamReader
        Dim SelectPolicy(9) As String
        Dim PolicyIdCode As String
        Dim i As Integer
        Dim looop As Integer

        PolicyIdCode = TextBox19.Text

        Input = File.OpenText("Policy Details.txt")

        line = Input.ReadLine()

        While line <> Nothing

            For looop = 1 To 15

                SelectPolicy = Split(line, ",")
                line = Input.ReadLine
                If PolicyIdCode = SelectPolicy(0) Then i = looop

            Next

        End While
        Input.Close()

        delete(i)

the i (int) is what line in the text file the specific line exists under, also for some reason i cant go into my delete sub procedure and im not quite sure how to do the actual deletion, this is the code that i have for the deletion itself

Dim objWriter As New System.IO.StreamWriter("Policy Details.txt", True)
        Dim SelectPolicy(9) As String
        Dim line As String
        line = " "
        SelectPolicy = Split(line, ",")



        SelectPolicy(0) = ""

        objWriter.WriteLine(SelectPolicy(0) & "," & SelectPolicy(0) & "," & SelectPolicy(0) & "," & SelectPolicy(0) & "," & SelectPolicy(0) & "," & SelectPolicy(0) & "," & SelectPolicy(0) & "," & SelectPolicy(0) & "," & SelectPolicy(0), i)

the selected policy is what im tryin to use to write over the line of texts that already exists however i think that maybe instead of deleteting what exists it will just move it down a line and make a blank line? ANY help would be greatly appreciated thanks

How about something like this.

Dim line As String
            Dim Input As StreamReader
            Dim PolicyIdCode As String
            Dim strFile As New ArrayList

            PolicyIdCode = TextBox19.Text

            Input = File.OpenText("Policy Details.txt")

            ' Loop through the file
            While Input.Peek <> -1
                ' Read the next available line
                line = Input.ReadLine

                ' Check to see if the line contains what you're looking for.
                ' If not, add it to an ArrayList
                If Not line.Contains(PolicyIdCode) Then
                    ' If not, add it to an ArrayList
                    strFile.Add(line)
                End If
            End While

            Input.Close()

            ' Because we want to replace the content of the file, we first
            ' need to delete it.
            If File.Exists("Policy Details.txt") Then
                File.Delete("Policy Details.txt")
            End If

            ' Create a StreamWriter object with the same filename
            Dim objWriter As New System.IO.StreamWriter("Policy Details.txt", True)
            ' Iterate through the ArrayList
            For Each item As String In strFile
                ' Write the current item in the ArrayList to the file.
                objWriter.WriteLine(item)
            Next
            objWriter.Flush()
            objWriter.Close()

There you have it.
Go through the file and add it, line by line, to an ArrayList skipping the lines you wish to remove.
Remove the file, and create a new file with the contents of the ArrayList.

cheers, got most the way through and i get it, i was trying something similar but dragging the whole document out into separate arrays.

This article has been dead for over six months. Start a new discussion instead.