0

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
yolad
  • 120
  • 10

4 Answers4

3

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
Seth Moore
  • 3,575
  • 2
  • 23
  • 33
1

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)
DAC84
  • 1,254
  • 1
  • 20
  • 30
1

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.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
  • 1
    He also would need to move his reading of all the fields to after the while loop, which is why he's getting data from the first line. – Seth Moore Jun 25 '13 at 13:57
  • @smoore That's why my first lines say shift the `While` loop to just after the first `Dim`. – Mark Hurd Jun 25 '13 at 13:59
  • Indeed it does! Sorry I didn't follow that right. +1 for you sir! – Seth Moore Jun 25 '13 at 14:00
  • Thanks for your help everyone! I Just had to add this line and take out the while loop Dim lastLine = File.ReadLines("C:\temp\Apr12log.txt").Last() – yolad Jun 25 '13 at 14:22
0

Try this:

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

lastLine = lines(lines.Length - 1)
SysDragon
  • 9,692
  • 15
  • 60
  • 89