We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,901 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Searching a text file, then deleting that specific line

I know how to search a text file for a specific line, but what I don't know is then how to delete that line? Can anyone help?

My text file is set up as such:

021,Donovan,56 Eynesford Crescent,Bexley,SE5 1TR,09/08/1967,13 March 2012,Bronze
062,Fredrikson,6 Freil Road,Gravesend,GR9 TRB,12/06/1995,13 March 2012,Silver

So I know how to search for the 3 character integer at the beginning of each line, but how do I then delete this line?

I'm currently using this code

Imports System.IO
Public Class Form6

    Private Sub SearchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBtn.Click

        Dim WholeFile() As String = System.IO.File.ReadAllLines("C:\Documents and Settings\User\My Documents\Visual Studio 2008\Projects\Members.txt")
        Dim Search As String = IDFilter.Text & ","

        For Each line As String In Filter(WholeFile, Search)
            If line.StartsWith(IDFilter.Text) Then
                MsgBox("found " & line)
            End If
        Next


    End Sub
End Class
3
Contributors
3
Replies
14 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
BeeKeeper18
Newbie Poster
9 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Please see this thread that you were posting in. The dictionary also supports a Remove method as in

records.Remove("095")

You could then (using the example in that other thread)

System.IO.File.WriteAllLines(outputfilename, records.Values.ToArray)

which creates an array of strings from the remaining values in the dictionary and writes them to a file.

Reverend Jim
Carpe per diem
Moderator
3,592 posts since Aug 2010
Reputation Points: 561
Solved Threads: 445
Skill Endorsements: 32

you can use replace option just replace the whole line with white space.

Regards

M.Waqas Aslam
Master Poster
743 posts since Aug 2011
Reputation Points: 50
Solved Threads: 120
Skill Endorsements: 2

That approach will work as long as you

1) use ReadAllText instead of ReadAllLines
2) include the trailing vbCrLf and replace the line with ""

Otherwise you will be left with a blank line. And to locate the record to delete you'd have to search for

vbCrLf & ID & ","

and do a check for the special case where the record to delete is the first one (with no preceding vbCrLf). All in all, a less robust and less clear approach.

Reverend Jim
Carpe per diem
Moderator
3,592 posts since Aug 2010
Reputation Points: 561
Solved Threads: 445
Skill Endorsements: 32

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0619 seconds using 2.66MB