2009-01-29 44 views
0

在SSIS VBScript中讀取文件的結尾的語法是什麼?如何在SSIS VBScript中指定EOF?

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不完全相同的閱讀到文件的末尾。但是我削減了很多,以至於我的代碼很差。

回答

1

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 
+0

好吧,這有點痛苦,因爲我真的想知道文件中最後一行的值,但我可以讓它工作。 – thursdaysgeek 2009-01-29 00:21:24

1

這裏只需要讀取的最後一行,而無需通過整個文件循環的方式。 它到文件末尾並開始向後讀取,直到它遇到另一個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