Как мне указать EOF в SSIS VBScript?
-
20-08-2019 - |
Вопрос
Каков синтаксис для чтения до конца файла в 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 - это не совсем то же самое, что чтение до конца файла.Но я сократил так много, что у меня получился плохой пример кода.
Решение
От 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
Другие советы
Вот способ прочитать только последнюю строку без циклического просмотра всего файла.Он переходит к концу файла и начинает чтение в обратном направлении, пока не наткнется на другой символ 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