vb.net edit a file based on criteria and save it

46 Views Asked by At

I have a file that has dates and results of tests, written line by line. I would like to edit the file such that any line that does not have today's date is deleted and then the file is saved (with the unwanted lines deleted). Help will be greatly appreciated.

Dim rawlines() As String Dim outputlines As New List(Of String) rawlines = File.ReadAllLines("C:\users\user10\rslts.csv") For Each line As String In rawlines If line.Contains(today()) = True Then outputlines.Add(line) End If Next

1

There are 1 best solutions below

0
On

Read Lines:

Dim rawlines() As String
    Dim outputlines As New List(Of String)
    rawlines = File.ReadAllLines("C:\users\user10\rslts.csv")
    For Each line As String In rawlines
        If line.Contains(today()) = True Then
            outputlines.Add(line)
        End If
    Next

Write lines to new file:

    Dim MyFile As StreamWriter
    File.Create("c:\test\TheResults.csv").Close()
    MyFile = File.AppendText("c:\test\TheResults.csv")
    For Each Line As String In outputlines
        MyFile.WriteLine(Line)
    Next
    MyFile.Close()