Sample code for 30+ languages & platforms
PowerBuilder

Read Text from a Remote SFTP File (Sequential)

See more SFTP Examples

Demonstrates the Chilkat SFtp.ReadFileText method, which reads up to a number of bytes from the current position of an open handle and decodes them to text. The arguments are the handle, the maximum number of bytes, and the charset. This example reads in chunks until end-of-file.

Background: Reading a file in bounded chunks keeps memory use predictable regardless of file size, which is why the loop reads a fixed amount and repeats until Eof. A subtlety worth knowing: the byte limit is applied to the encoded bytes, so a chunk boundary could in principle fall in the middle of a multibyte character — accumulating the pieces and decoding the whole avoids any such issue in practice here since the text is reassembled in a StringBuilder.

Chilkat PowerBuilder Downloads

PowerBuilder
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.ReadFileText method, which reads up to a number of bytes from the current
//  position of an open handle and decodes them to text.  The 1st argument is the handle, the 2nd
//  is the maximum number of bytes, and the 3rd is the charset.

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

//  Open the remote file for reading.
ls_Handle = loo_Sftp.OpenFile("subdir/notes.txt","readOnly","openExisting")
if loo_Sftp.LastMethodSuccess = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Read the file in chunks until end-of-file, accumulating the text.
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)

    //  Stop once the end of the file has been reached.
    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