Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete from TXT file

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi

What is the best approach to search a txt file for a specific piece of text and when found delete that line and the next 5 lines from the TXT file

I have code to delete the line that contained the searched text but lost on how to delete the next 5 lines.

Thanks
 
Always post what you have. To help you it is almost always better to help you fix what you have already done. It should really already be apparent what you would do. If you don't know I would really have to see your code to understand how you are lost.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Here is the code that i have at the momment

Code:
 Dim all_lines As Array
        Dim lines As New List(Of String)

        all_lines = File.ReadAllLines("c:\test.txt")
        lines.AddRange(all_lines)
        Array.Clear(all_lines, 0, Text.Length)
        lines.RemoveAt(lines.IndexOf("line2_remove"))
        all_lines = lines.ToArray()
        File.WriteAllLines("c:\test.txt", all_lines)
 
Code:
        Dim all_lines As Array
        Dim lines As New List(Of String)
        Dim IndexFound As Integer

        all_lines = File.ReadAllLines("c:\test.txt")
        lines.AddRange(all_lines)
        Array.Clear(all_lines, 0, Text.Length)
        [red]IndexFound = lines.IndexOf("line2_remove")[/red]

        For count As Integer = 0 To 5
            lines.RemoveAt([red]IndexFound[/red])
        Next

        all_lines = lines.ToArray()
        File.WriteAllLines("c:\test.txt", all_lines)
That is of course only if it will always be the next 5 lines.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top