PowerBuilder
PowerBuilder
Open a Remote File on an SFTP Server
See more SFTP Examples
Demonstrates the Chilkat SFtp.OpenFile method, which opens or creates a remote file and returns a handle for subsequent read or write operations. The arguments are the remote path, the access mode (readOnly, writeOnly, or readWrite), and the create disposition. Close the handle with CloseHandle when finished.
Background: Handle-based access is the foundation for everything the by-name convenience methods hide. The create disposition is the key control — it decides whether a missing file is an error (
openExisting), whether an existing file is truncated (createTruncate) or preserved (openOrCreate), and can add options such as appendData for logging. Because the handle names a server-side resource, it must be closed to avoid exhausting the server's open-file limit.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
string ls_Handle
li_Success = 0
// Demonstrates the SFtp.OpenFile method, which opens or creates a remote file and returns a
// handle for subsequent read or write operations. The 1st argument is the remote path, the 2nd
// is the access mode, and the 3rd is the create disposition.
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
// access must be "readOnly", "writeOnly", or "readWrite".
//
// createDisposition is a comma-separated list containing exactly one primary disposition:
// createNew Create a new file; fail if it already exists.
// createTruncate Create a new file, or open and truncate an existing file.
// openExisting Open an existing file; fail if it does not exist.
// openOrCreate Open the file if it exists, otherwise create it.
// truncateExisting Open and truncate an existing file; fail if it does not exist.
// Optional additional keywords include appendData, textMode, blockRead, blockWrite, and others.
// Open a remote file for reading.
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
Write-Debug "Opened file, handle = " + ls_Handle
// ... read from the handle ...
// Always close the handle when finished.
li_Success = loo_Sftp.CloseHandle(ls_Handle)
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
end if
loo_Sftp.Disconnect()
destroy loo_Sftp