none
Delete Line RRS feed

  • Question

  • Hello, all im trying to delete 1 line in update.txt file.

    How can i  make to search for http://www.website.com if exist remove line else nothing.

    I need to remove the line not replace by ""

    Thanks



    • Edited by extream87 Friday, January 8, 2016 2:12 PM
    Friday, January 8, 2016 2:11 PM

Answers

  • Try this

    Dim fileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data.txt")
    Dim lines As String() = IO.File.ReadAllLines(fileName)
    IO.File.WriteAllLines(
        fileName,
        (
            From line In lines
            Where Not line.ToLower.Contains("http://www.website.com")
            Select line).ToArray
        )

    Data.txt before

    http://www.google.com
    http://www.website.com
    http://www.microsoft.com

    After

    http://www.google.com
    http://www.microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter or Facebook via my MSDN profile but will not answer coding question on either.

    • Proposed as answer by Blackwood Friday, January 8, 2016 3:34 PM
    • Marked as answer by extream87 Friday, January 8, 2016 5:03 PM
    Friday, January 8, 2016 2:23 PM
    Moderator

All replies

  • Try this

    Dim fileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data.txt")
    Dim lines As String() = IO.File.ReadAllLines(fileName)
    IO.File.WriteAllLines(
        fileName,
        (
            From line In lines
            Where Not line.ToLower.Contains("http://www.website.com")
            Select line).ToArray
        )

    Data.txt before

    http://www.google.com
    http://www.website.com
    http://www.microsoft.com

    After

    http://www.google.com
    http://www.microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter or Facebook via my MSDN profile but will not answer coding question on either.

    • Proposed as answer by Blackwood Friday, January 8, 2016 3:34 PM
    • Marked as answer by extream87 Friday, January 8, 2016 5:03 PM
    Friday, January 8, 2016 2:23 PM
    Moderator
  • Sub DeleteLine()
            Dim line As String
            Dim newLine As New StringBuilder
            Using stream As New StreamReader("D:\temp\Download.txt")
                Do
                    line = stream.ReadLine()
                    If Not String.IsNullOrEmpty(line) Then
                        If Not line.Contains("Text to Delete") Then
                            newLine.AppendLine(line)
                        Else
                            Dim site As New ProcessStartInfo("http://website.com")
                            Process.Start(site)
                        End If
                    End If
                Loop Until line Is Nothing
                stream.Close()
            End Using
            Using writer As New StreamWriter("D:\temp\Download.txt")
                writer.Write(newLine.ToString())
                writer.Close()
            End Using
        End Sub


    It all Happenz Sendil

    Friday, January 8, 2016 2:52 PM