代码之家  ›  专栏  ›  技术社区  ›  thursdaysgeek

如何在SSIS VBScript中指定EOF?

  •  0
  • thursdaysgeek  · 技术社区  · 16 年前

    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
    

    编辑:我实际上是想得到文件中最后一行的值。因此,读取到not EOF与读取到文件末尾并不完全相同。但是我剪的太多了,所以我的示例代码很差。

    2 回复  |  直到 16 年前
        1
  •  1
  •   Cade Roux    16 年前

    从…起 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
    

    如果您只需要最后一行:

    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
  •  1
  •   Ray Dai    15 年前

    它转到文件的末尾并开始向后读取,直到它碰到另一个LF字符,这表示从第二行到最后一行的末尾,然后它只读取该行。

    在具有数百万行的大文件上,这降低了读取几个字节的成本。

        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
    
    推荐文章