Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
string ls_Handle
oleobject loo_Bd
integer li_ChunkSize
integer li_Reading
integer li_LastCount

li_Success = 0

//  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.

loo_Sftp = create oleobject
li_rc = loo_Sftp.ConnectToNewObject("Chilkat.SFtp")
if li_rc < 0 then
    destroy loo_Sftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Connect, authenticate, and initialize the SFTP subsystem.
li_Port = 22
li_Success = loo_Sftp.Connect("sftp.example.com",li_Port)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_Sftp.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

ls_Handle = loo_Sftp.OpenFile("subdir/data.txt","readOnly","openExisting")
if loo_Sftp.LastMethodSuccess = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_ChunkSize = 4096
li_Reading = 1
do while li_Reading
    li_Success = loo_Sftp.ReadFileBd(ls_Handle,li_ChunkSize,loo_Bd)
    if loo_Sftp.LastReadFailed(ls_Handle) then
        Write-Debug loo_Sftp.LastErrorText
        destroy loo_Sftp
        destroy loo_Bd
        return
    end if

    //  Report how many bytes the last read actually returned.  A normal EOF read reports 0.
    li_LastCount = loo_Sftp.LastReadNumBytes(ls_Handle)
    Write-Debug "Last read returned " + string(li_LastCount) + " bytes."

    li_Reading = not loo_Sftp.Eof(ls_Handle)
loop

li_Success = loo_Sftp.CloseHandle(ls_Handle)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    destroy loo_Bd
    return
end if

Write-Debug "Total: " + string(loo_Bd.NumBytes) + " bytes."

loo_Sftp.Disconnect()


destroy loo_Sftp
destroy loo_Bd