0

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

編集:実際には、ファイルの最後の行の値を取得しようとしています。そのため、EOF ではないまで読み取ることは、ファイルの最後まで読み取ることとまったく同じではありません。しかし、私はあまりにも多くをカットしたので、サンプルコードは貧弱でした。

4

2 に答える 2

1

ファイル全体をループせずに最後の行だけを読み取る方法を次に示します。ファイルの最後に移動し、最後から 2 番目の行の終わりを示す別の LF 文字に到達するまで逆方向に読み取りを開始し、その行を読み取ります。

数百万行の大きなファイルでは、これにより数バイトの読み取りコストが削減されます。

出力ウィンドウで何が起こっているか、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
于 2009-12-30T19:25:50.010 に答える