Sample code for 30+ languages & platforms
DataFlex

Get the Byte Count of the Last SFTP Read

See more SFTP Examples

Demonstrates the Chilkat SFtp.LastReadNumBytes method, which returns the number of bytes received by the most recent read for a handle. The only argument is the handle.

Background: SFTP reads may return fewer bytes than requested even before the end of the file, so knowing the actual count is useful for tracking progress and for advancing an explicit offset by the right amount. Both a normal EOF read and a failed read report 0, so pair this with Eof and LastReadFailed to interpret a zero-byte result correctly.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSftp
    Integer iPort
    String sPassword
    String sHandle
    Variant vBd
    Handle hoBd
    Integer iChunkSize
    Boolean iReading
    Integer iLastCount
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Demonstrates the SFtp.LastReadNumBytes method, which returns the number of bytes received by
    //  the most recent read for a handle.  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

    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Move 4096 To iChunkSize
    Move True To iReading
    While iReading
        Get pvComObject of hoBd to vBd
        Get ComReadFileBd Of hoSftp sHandle iChunkSize vBd To iSuccess
        Get ComLastReadFailed Of hoSftp sHandle To bTemp1
        If (bTemp1) Begin
            Get ComLastErrorText Of hoSftp To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        //  Report how many bytes the last read actually returned.  A normal EOF read reports 0.
        Get ComLastReadNumBytes Of hoSftp sHandle To iLastCount
        Showln "Last read returned " iLastCount " bytes."

        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 ComNumBytes Of hoBd To iTemp1
    Showln "Total: " iTemp1 " bytes."

    Send ComDisconnect To hoSftp


End_Procedure