Results 1 to 3 of 3

Thread: [RESOLVED] VB.NET search text file and remove only first found duplicate.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Resolved [RESOLVED] VB.NET search text file and remove only first found duplicate.

    Hi All - I am using this code to find and remove duplicates, but now what I need to do is search for the duplicate, but only remove the first duplicate found in the text file. I could have up to 3-4 duplicates in the file. Having a hard time searching for this.

    Code:
    Dim filename As String = "c:\scripts\scan.txt"
    File.WriteAllLines(filename, File.ReadAllLines(filename).Where(Function(l) l <> TB_Search.Text))

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: VB.NET search text file and remove only first found duplicate.

    Your title is a bit different than what your code is doing. It looks like you want to remove only the first instance of a line in a text file that matches the text in your TextBox.

    If that's the case, then you can do this:
    Code:
    Private Sub RemoveMatchingLineFromFile(filename As String, term As String)
        Dim lines = New List(Of String)(IO.File.ReadAllLines(filename))
    
        Dim indexToRemove = -1
        For index = lines.Count - 1 To 0 Step -1
            Dim line = lines(index)
            If (line = term)) Then
                indexToRemove = index
            End If
        Next
    
        If (indexToRemove > -1) Then
            lines.RemoveAt(indexToRemove)
        End If
    
        IO.File.WriteAllLines(filename, lines)
    End Sub
    
    ' RemoveMatchingLineFromFile("c:\scripts\scan.txt", TB_Search.Text)
    This loops from the last line to the first line, sets the index that will be removed if the currently iterated line matches the search term, optionally removes the line, and then updates the file's text.

    Edit - I just realized you can make this more concise by using the FindIndex method:
    Code:
    Private Sub RemoveMatchingLineFromFile(filename As String, term As String)
        Dim lines = New List(Of String)(IO.File.ReadAllLines(filename))
    
        Dim indexToRemove = lines.FindIndex(Function(line) line = term)
        If (indexToRemove > -1) Then
            lines.RemoveAt(indexToRemove)
        End If
    
        IO.File.WriteAllLines(filename, lines)
    End Sub
    It's the same concept only the looping is taken care of under the hood.
    Last edited by dday9; Oct 17th, 2023 at 09:34 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: VB.NET search text file and remove only first found duplicate.

    dday9 - Thank you, this is EXACTLY what I needed. Much appreciated

Tags for this Thread

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