Xbase++
Xbase++
Download Text from an SFTP Server (StringBuilder)
See more SFTP Examples
Demonstrates the Chilkat SFtp.DownloadSb method, which downloads a remote text file, decodes it with the given charset, and appends the text to a StringBuilder. The arguments are the remote file path, the charset, and the StringBuilder.
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: When the remote file is text and you want its content as characters rather than raw bytes, this decodes it in one step so you can parse or display it directly. The charset must match how the file is actually encoded, or the text will be garbled. Like
DownloadBd it appends, so clear the StringBuilder first if you want only this file's text.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oSftp
LOCAL nPort
LOCAL cPassword
LOCAL oSb
LOCAL cFileText
nSuccess := 0
// Demonstrates the SFtp.DownloadSb method, which downloads a remote text file, decodes it, and
// appends the text to a StringBuilder. The 1st argument is the remote file path, the 2nd is the
// charset, and the 3rd is the StringBuilder.
oSftp := CreateObject("Chilkat.SFtp")
// Connect, authenticate, and initialize the SFTP subsystem.
nPort := 22
nSuccess := oSftp:Connect("sftp.example.com", nPort)
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// 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.
cPassword := "mySshPassword"
nSuccess := oSftp:AuthenticatePw("mySshLogin", cPassword)
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
nSuccess := oSftp:InitializeSftp()
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// Download the remote text file, decoding it as UTF-8. Existing text in the StringBuilder is
// preserved, so clear it first if you want replacement rather than append behavior.
oSb := CreateObject("Chilkat.StringBuilder")
nSuccess := oSftp:DownloadSb("subdir/inventory.csv", "utf-8", oSb)
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
oSb:destroy()
RETURN
ENDIF
? "Characters downloaded: " + Str(oSb:Length)
cFileText := oSb:GetAsString()
? cFileText
oSftp:Disconnect()
oSftp:destroy()
oSb:destroy()