views:

1602

answers:

2

What is the syntax for reading until the end of file in SSIS VBScript?

Dim readFile As FileInfo = New FileInfo(logHourlyName)
If readFile.Exists() Then
   Dim textStream As StreamReader = readFile.OpenText()
   Dim strLine As String
   Do While Not EOF    <--- what goes here?
       curLine = textStream.ReadLine()
   Loop
   textStream.Close()
End If

Edit: I'm actually trying to get the value of the last line in the file. So reading until not EOF is not quite the same as reading to the end of the file. But I cut so much that I had poor example code.

+1  A: 

From http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx:

Dim readFile As FileInfo = New FileInfo(logHourlyName)
If readFile.Exists() Then
   Dim textStream As StreamReader = readFile.OpenText()
   Dim strLine As String
   Do
       curLine = textStream.ReadLine()
   Loop Until curLine Is Nothing
   textStream.Close()
End If

If you just want the last line:

Dim readFile As FileInfo = New FileInfo(logHourlyName)
Dim lastLine As String
If readFile.Exists() Then
   Dim textStream As StreamReader = readFile.OpenText()
   Dim strLine As String
   Do
       curLine = textStream.ReadLine()
       If Not curLine Is Nothing Then lastLine = curLine
   Loop Until curLine Is Nothing
   textStream.Close()
End If
Cade Roux
Ok, that's somewhat of a pain, since I really want to know the value of the last line in the file, but I can make it work.
thursdaysgeek
So, another variable to hold what the current line is before it equals nothing. That will work. Thank you.
thursdaysgeek
A: 

Here is a way to read just the last line without looping through the entire file. It goes to the end of the file and start reading backwards until it hits another LF character, which indicates the end of the second to last line, and it then just read that line in.

On a big file with millions of rows, this decrease the cost to reading few bytes.

You can uncomment Dts.Events.FireInformation codes what is happening in your output window.

    Dim i As Integer
    Dim CurrentByte As Integer
    Dim Trailer As String

    i = 1

    Using reader As StreamReader = New StreamReader("c:\temp\SourceFile.txt")
        Do While CurrentByte <> 10 'while we are not finding the next LF character
           reader.BaseStream.Seek((-1 * i) - 2, SeekOrigin.End) 'seeking backwards from the last position in the file minus the last CRLF
            'Dts.Events.FireInformation(0, "Now at position", reader.BaseStream.Position().ToString, "", 0, False)
            CurrentByte = reader.BaseStream.ReadByte 'read the next byte, this will advance pointer position
            'Dts.Events.FireInformation(0, "Current ASCII Code", CurrentByte & " Character:" & Chr(CurrentByte), "", 0, False)
            i = i + 1 'go to the next character                 
        Loop
        Trailer = reader.ReadLine 'we exited on the LF character, so we are at the beginning of trailer line
        Dts.Events.FireInformation(0, "   Trailer:", Trailer, "", 0, False)
    End Using
Ray Dai