문제

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top