Sample code for 30+ languages & platforms
PowerBuilder

Upload Text to an SFTP Server (StringBuilder)

See more SFTP Examples

Demonstrates the Chilkat SFtp.UploadSb method, which encodes the text in a StringBuilder and uploads it to a remote file. The arguments are the StringBuilder, the remote file path, the charset, and whether to prepend a byte-order mark.

Note: This example uses relative local paths, which are resolved against the application's current working directory. Absolute local paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the text-oriented upload: you assemble a document — a CSV, a config file, JSON — in a StringBuilder and the method handles character encoding as it writes. The charset controls the bytes actually stored (for example utf-8 or iso-8859-1), and the byte-order-mark flag is useful when a consuming Windows application expects a BOM. An existing destination is truncated first.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
oleobject loo_Sb
integer li_IncludeBom

li_Success = 0

//  Demonstrates the SFtp.UploadSb method, which encodes the text in a StringBuilder and uploads
//  it to a remote file.  The 1st argument is the StringBuilder, the 2nd is the remote file path,
//  the 3rd is the charset, and the 4th selects whether a byte-order mark is prepended.

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

//  Build the text to upload.
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")

loo_Sb.Append("name,quantity~n")
loo_Sb.Append("widgets,42~n")
loo_Sb.Append("gadgets,17~n")

//  Upload as UTF-8 without a byte-order mark.  If the remote file exists, it is truncated first.
li_IncludeBom = 0
li_Success = loo_Sftp.UploadSb(loo_Sb,"subdir/inventory.csv","utf-8",li_IncludeBom)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    destroy loo_Sb
    return
end if

Write-Debug "Uploaded text file."

loo_Sftp.Disconnect()


destroy loo_Sftp
destroy loo_Sb