PowerBuilder
PowerBuilder
Close a Remote SFTP File or Directory Handle
See more SFTP Examples
Demonstrates the Chilkat SFtp.CloseHandle method, which closes a remote file or directory handle previously returned by OpenFile or OpenDir. The only argument is the handle.
Background: Every opened handle corresponds to state the server holds, and servers cap how many a session may keep open at once, so a long-running program that forgets to close handles will eventually start failing to open new ones. Closing also ensures buffered writes are committed. Once closed, a handle is invalid and must not be reused.
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.CloseHandle method, which closes a remote file or directory handle. The
// only argument is the handle returned by OpenFile or OpenDir.
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 a remote file, obtaining a handle.
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
// ... use the handle ...
// Close the handle to release the server-side resource. A handle should not be used after it
// is closed.
li_Success = loo_Sftp.CloseHandle(ls_Handle)
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
end if
Write-Debug "Handle closed."
loo_Sftp.Disconnect()
destroy loo_Sftp