Sample code for 30+ languages & platforms
DataFlex

Detect End-of-File on a Remote SFTP File

See more SFTP Examples

Demonstrates the Chilkat SFtp.Eof method, which returns whether the most recent read for a handle received the SFTP end-of-file status. The only argument is the handle. This example reads until Eof is true.

Background: The timing here is worth understanding: reading exactly through the final byte does not immediately set EOF. The next read succeeds, returns zero bytes, sets the status to SSH_FX_EOF, and only then does Eof report true. That is why the loop tests Eof after each read rather than assuming a short read means the end — a short read can also just be the server returning less than requested.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSftp
    Integer iPort
    String sPassword
    String sHandle
    Handle hoSbContent
    Integer iChunkSize
    Boolean iReading
    String sChunk
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Demonstrates the SFtp.Eof method, which returns whether the most recent read for a handle
    //  received the SFTP end-of-file status.  The only argument is the handle.

    Get Create (RefClass(cComChilkatSFtp)) To hoSftp
    If (Not(IsComObjectCreated(hoSftp))) Begin
        Send CreateComObject of hoSftp
    End

    //  Connect, authenticate, and initialize the SFTP subsystem.
    Move 22 To iPort
    Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComInitializeSftp Of hoSftp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComOpenFile Of hoSftp "subdir/data.txt" "readOnly" "openExisting" To sHandle
    Get ComLastMethodSuccess Of hoSftp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Read until Eof reports the end of the file.  Note that reading exactly through the final byte
    //  does not set EOF; the next read returns zero bytes and then Eof becomes true.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbContent
    If (Not(IsComObjectCreated(hoSbContent))) Begin
        Send CreateComObject of hoSbContent
    End
    Move 4096 To iChunkSize
    Move True To iReading
    While iReading
        Get ComReadFileText Of hoSftp sHandle iChunkSize "utf-8" To sChunk
        Get ComLastReadFailed Of hoSftp sHandle To bTemp1
        If (bTemp1) Begin
            Get ComLastErrorText Of hoSftp To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComAppend Of hoSbContent sChunk To iSuccess
        Get ComEof Of hoSftp sHandle To bTemp1
        Move (Not bTemp1) To iReading
    Loop

    Get ComCloseHandle Of hoSftp sHandle To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetAsString Of hoSbContent To sTemp1
    Showln sTemp1

    Send ComDisconnect To hoSftp


End_Procedure