PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
string ls_Handle
oleobject loo_SbContent
integer li_ChunkSize
integer li_Reading
string ls_Chunk
li_Success = 0
// 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.
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
// 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.
loo_SbContent = create oleobject
li_rc = loo_SbContent.ConnectToNewObject("Chilkat.StringBuilder")
li_ChunkSize = 4096
li_Reading = 1
do while li_Reading
ls_Chunk = loo_Sftp.ReadFileText(ls_Handle,li_ChunkSize,"utf-8")
if loo_Sftp.LastReadFailed(ls_Handle) then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
destroy loo_SbContent
return
end if
loo_SbContent.Append(ls_Chunk)
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_SbContent
return
end if
Write-Debug loo_SbContent.GetAsString()
loo_Sftp.Disconnect()
destroy loo_Sftp
destroy loo_SbContent