This may work for you: it's sparse for clarity of concept, and assumes that
there are NO extra characters after the last line (no spaces, newlines,
nothing!). You really should add error handling to this, as it assumes too
many things - I've left it out again for concept. (It assumes the file
exists, that it has more than one line, etc)
Imports System.IO
Module Module1
Sub Main()
Dim firstLine As String = Nothing
Dim lastLine As String = Nothing
Dim sb As New System.Text.StringBuilder
Dim readCount As Integer
Dim readByte As Integer
Dim br As New System.IO.FileStream("c:\test.txt", FileMode.Open)
readByte = br.ReadByte()
While readByte <> -1 AndAlso readByte <> 13
sb.Append(Convert.ToChar(readByte))
readCount += 1
readByte = br.ReadByte()
End While
firstLine = sb.ToString()
sb.Length = 0
br.Seek(-1 * readCount, SeekOrigin.End)
readByte = br.ReadByte()
While readByte <> -1 AndAlso readByte <> 13
sb.Append(Convert.ToChar(readByte))
readByte = br.ReadByte()
End While
lastLine = sb.ToString()
If String.Compare(lastLine, firstLine, True) = 0 Then
Console.WriteLine("They Match")
Else
Console.WriteLine("They DO NOT Match")
End If
End Sub
End Module
"Catalin Porancea" <ca**************@midwestern.net> wrote in message
news:OO**************@TK2MSFTNGP11.phx.gbl...
Thanks Philip.
Isn't there a way to send the reader directly to the last line?
"Philip Rieck" <st***@mckraken.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl... This console app code may help, it does what you ask. Create a new
Console application, and paste this in to Module1.vb, replacing whats already
there.
Imports System.IO
Module Module1
Sub Main()
Dim firstLine As String = Nothing
Dim lastLine As String = Nothing
Dim tmpLine As String = String.Empty
Dim sr As StreamReader = New StreamReader("c:\test.txt")
firstLine = sr.ReadLine()
While Not tmpLine Is Nothing
lastLine = tmpLine
tmpLine = sr.ReadLine()
End While
sr.Close()
If String.Compare(lastLine, firstLine, True) = 0 Then
Console.WriteLine("They Match")
Else
Console.WriteLine("They DO NOT Match")
End If
End Sub
End Module
"Catalin Porancea" <ca**************@midwestern.net> wrote in message
news:OT****************@TK2MSFTNGP10.phx.gbl... Hello,
I have a txt file that I need to validate before importing in a database. In order to be valid, the file needs to have a header, records and a trailer. How do I read only the first and the last line in the file and load
them in 2 strings so I can compare them?
Thank you.
Catalin