如何在SSIS VBScript中指定EOF?

时间:2021-10-06 05:07:56

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

在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.

编辑:我实际上是想获取文件中最后一行的值。因此,读取直到不EOF与读取到文件末尾不完全相同。但是我削减了很多,以至于我的示例代码很差。

2 个解决方案

#1


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

#2


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.

这是一种只读取最后一行而不循环遍历整个文件的方法。它到达文件的末尾并开始向后读取,直到它到达另一个LF字符,表示第二行到最后一行的结束,然后它才读取该行。

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.

您可以取消注释Dts.Events.FireInformation代码输出窗口中发生的事情。

    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

#1


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

#2


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.

这是一种只读取最后一行而不循环遍历整个文件的方法。它到达文件的末尾并开始向后读取,直到它到达另一个LF字符,表示第二行到最后一行的结束,然后它才读取该行。

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.

您可以取消注释Dts.Events.FireInformation代码输出窗口中发生的事情。

    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