Results 1 to 9 of 9

Thread: [RESOLVED]Deleting a line in a text file that ends with a certain string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    [RESOLVED]Deleting a line in a text file that ends with a certain string

    Hey,

    I need help deleting a line from a text file. This is for an ASP.net Webpage. I will have a file called Sources.txt. Example data of this file looks like this:

    John|[email protected]
    Bob|[email protected]

    I would like to delete the line ending in |[email protected] programmatically. This will obviously be dynamic. I want to do this by determining what line number it is at, unless of course, there is a better way.

    I want to search for the line by the ending of it so people cannot put [email protected]|[email protected] and accidentally get [email protected] deleted.

    This is for an unsubscribe link.

    Code:
            Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
            lines.RemoveAt(1)
            IO.File.WriteAllLines("Sources.txt", lines.ToArray())
            'Stop people from refreshing and deleting other records.
            Session.Clear()
        End Sub
    Thanks
    Last edited by samtheman; Jul 9th, 2009 at 06:44 PM.
    If you found any of my posts helpful then please rate them.

    CodeBank

    Form Fading Effects in VB.NET and C#

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Deleting a line in a text file that ends with a certain string

    Have you looked at String.EndsWith ?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: Deleting a line in a text file that ends with a certain string

    Hi,

    This will not work with a list. Instead I shall try using List.Find, letting you know how I get on.
    If you found any of my posts helpful then please rate them.

    CodeBank

    Form Fading Effects in VB.NET and C#

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: Deleting a line in a text file that ends with a certain string

    Ok, I have found I can get it to work. But I need help

    Code:
            Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
            Dim DataList As String = lines.ToString
            If DataList.EndsWith("|[email protected]") Then
                lines.....
            End If
    I want to go through the entry and anything matching If DataList.EndsWith("|[email protected]"), I want to get the Index and then delete it with

    Code:
    lines.RemoveAt(1)
    I then want to keep going through it so I scan the whole document.
    If you found any of my posts helpful then please rate them.

    CodeBank

    Form Fading Effects in VB.NET and C#

  5. #5
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Deleting a line in a text file that ends with a certain string

    Why don't you go trough each line and if that line doesnt endswith that string, just add the string to a new list?

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: Deleting a line in a text file that ends with a certain string

    Hey,

    This seems a little RAM inefficient, but it seems to be the only way, I tried to do an If String.Endswith = .... then GetIndexOf but it failed.

    Can you give me some code to suggest this method of yours? Baring in mind after removal I was planning to recreate the file with
    vb Code:
    1. IO.File.WriteAllLines("Sources.txt", lines.ToArray())
    and that there may be several R/Ws going on at once.
    If you found any of my posts helpful then please rate them.

    CodeBank

    Form Fading Effects in VB.NET and C#

  7. #7
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Deleting a line in a text file that ends with a certain string

    Try this:

    VB.NET Code:
    1. Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
    2.         Dim finalLines As New List(Of String)
    3.         For Each line As String In lines
    4.             If Not line.EndsWith("|[email protected]") Then
    5.                 finalLines.Add(line)
    6.             End If
    7.         Next

    Good luck
    Last edited by mickey_pt; Jul 9th, 2009 at 06:25 PM. Reason: Code...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: Deleting a line in a text file that ends with a certain string

    Will try after a 10 minute break
    If you found any of my posts helpful then please rate them.

    CodeBank

    Form Fading Effects in VB.NET and C#

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    Re: Deleting a line in a text file that ends with a certain string

    Perfect my good man, will give you rep and mark as resolved. I added this: IO.File.WriteAllLines("Sources.txt", finalLines.ToArray()) so that I write the final lines to the TXT file.

    Code:
            Dim lines As New List(Of String)(IO.File.ReadAllLines("Sources.txt"))
            Dim finalLines As New List(Of String)
            For Each line As String In lines
                If Not line.EndsWith("|[email protected]") Then
                    finalLines.Add(line)
                End If
            Next
            IO.File.WriteAllLines("Sources.txt", finalLines.ToArray())
        End Sub
    If you found any of my posts helpful then please rate them.

    CodeBank

    Form Fading Effects in VB.NET and C#

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width