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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete line from text file

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
I need to be able to search a text file for several unique line and then remove only them. I know how to make a StreamReader and a StreamWriter, but if either can do this I didn't find it.

-I hate Microsoft!
-Forever and always forward.
 
Open the file via Streamreader, read each line into a variable, compare for your "line to remove", write out as necessary...

Code:
Dim sw As New StreamWriter([i]path to outputfile[/i])
Using sr As New StreamReader([i]path to inputfile[/i])
    Do
        Dim strLine As String = sr.ReadLine
        If strLine.Substring(0, 4) <> "test" Then [i]'modify the search for lines you want to exclude here[/i]
            sw.WriteLine(strLine)
        End If
    Loop Until sr.EndOfStream
    sr.Close
End Using
sw.Close

or somethign very similar.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Thanks. So if I understand I have no choice and if I wanted to read and write to the same file I'm stuck with reading the full file in and then over write the file or write to a temp file and then copy over the old file.

-I hate Microsoft!
-Forever and always forward.
 
That is my experience Sorwen. There may be another way, but not one I know of. I wouldn't think it would be a major impact though, unless the files are super long or you have a slow network connection (such as WAN or wireless) and working with remote files. Just make sure you properly close up the files to avoid locks and leaks.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
I wouldn't think it would be a major impact though, unless the files are super long or you have a slow network connection and working with remote files."

All of the above sometimes. Our network in this building is pretty bad.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top