Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Demonstrates the SFtp.CloseHandle method, which closes a remote file or directory handle.  The
;  only argument is the handle returned by OpenFile or OpenDir.

$oSftp = ObjCreate("Chilkat.SFtp")

;  Connect, authenticate, and initialize the SFTP subsystem.
Local $iPort = 22
$bSuccess = $oSftp.Connect("sftp.example.com",$iPort)
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
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.
Local $sPassword = "mySshPassword"

$bSuccess = $oSftp.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSftp.InitializeSftp()
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

;  Open a remote file, obtaining a handle.
Local $sHandle = $oSftp.OpenFile("subdir/data.txt","readOnly","openExisting")
If ($oSftp.LastMethodSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

;  ... use the handle ...

;  Close the handle to release the server-side resource.  A handle should not be used after it
;  is closed.
$bSuccess = $oSftp.CloseHandle($sHandle)
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("Handle closed." & @CRLF)

$oSftp.Disconnect