yolad
yolad

Reputation: 120

Importing last line of a .csv file

I have a csv file and I need to get the last line only into seperate textboxes.

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub btnConditions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConditions.Click
        Using reader As New StreamReader("C:\temp\Apr12log.txt")
            Dim line As String = reader.ReadLine()


            Dim fields() As String = line.Split(",".ToCharArray())
            Dim fileDate = CDate(fields(0))
            Dim fileTime = fields(1)
            Dim fileTemp = fields(2)
            Dim fileHum = fields(3)
            Dim fileWindSpeed = fields(4)
            Dim fileWindGust = fields(5)
            Dim fileWindBearing = fields(6)

            While line IsNot Nothing

                line = reader.ReadLine()
            End While
            txtDate.Text = CStr(fileDate)
        End Using

    End Sub

End Class

It only inputs the first line I am not sure how to get the last line only.

example of txtfile

01/04/12,00:00,5.4,80,3.0,4.5,9.6,261,0.0,0.0,1025.0,1.0,16.8,43,4.0,3.8,5.4,0.0,0,0.0

Upvotes: 0

Views: 2117

Answers (4)

Seth Moore
Seth Moore

Reputation: 3545

Alternatively, you could use the System.IO.File ReadLines() function, which gives you an enumerable that you can call Last() on.

Private Sub btnConditions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConditions.Click

        Dim line As String = System.IO.File.ReadLines("C:\temp\Apr12log.txt").Last()

        Dim fields() As String = line.Split(",".ToCharArray())
        Dim fileDate = CDate(fields(0))
        Dim fileTime = fields(1)
        Dim fileTemp = fields(2)
        Dim fileHum = fields(3)
        Dim fileWindSpeed = fields(4)
        Dim fileWindGust = fields(5)
        Dim fileWindBearing = fields(6)

        txtDate.Text = CStr(fileDate)

End Sub

Upvotes: 3

Mark Hurd
Mark Hurd

Reputation: 10931

You can get what you want with a couple of edits to your existing code:

Shift

   While line IsNot Nothing

        line = reader.ReadLine()
   End While

To just after

   Dim line As String = reader.ReadLine()

Add another

   Dim line2 As String = Nothing

And insert

       line2 = line

Into the While loop, i.e.

   While line IsNot Nothing
        line2 = line
        line = reader.ReadLine()
   End While

Now line2 is the last line in the file.

Upvotes: 1

DAC84
DAC84

Reputation: 1274

You're only reading in one line. Instead use ReadToEnd() and then split by a new line, like so:

Dim lines() As String = reader.ReadToEnd().Split(Environment.NewLine)

Then you can move to the last line:

Dim line As String = lines(lines.Length - 1)

Upvotes: 1

SysDragon
SysDragon

Reputation: 9888

Try this:

Dim lines() As String = File.ReadAllLines("C:\temp\Apr12log.txt")

lastLine = lines(lines.Length - 1)

Upvotes: 0

Related Questions