-1

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

MordC
  • 61
  • 3
  • 11
  • There are a lot of potential issues with your code, but assuming that it runs and stores the data in `outputlines`, simply loop through the list and write the lines to a new file. – Prescott Chartier Oct 12 '17 at 14:56
  • @PrescottChartier how do I write do the looping through and writing to the new file. help will be greatly appreciated, please. – MordC Oct 13 '17 at 10:42

1 Answers1

0

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()
Prescott Chartier
  • 1,519
  • 3
  • 17
  • 34