Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSftp
LOCAL lnPort
LOCAL lcPassword
LOCAL lcHandle

lnSuccess = 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.

loSftp = CreateObject('Chilkat.SFtp')

*  Connect, authenticate, and initialize the SFTP subsystem.
lnPort = 22
lnSuccess = loSftp.Connect("sftp.example.com",lnPort)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
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.
lcPassword = "mySshPassword"

lnSuccess = loSftp.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

lnSuccess = loSftp.InitializeSftp()
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

*  Open a remote file, obtaining a handle.
lcHandle = loSftp.OpenFile("subdir/data.txt","readOnly","openExisting")
IF (loSftp.LastMethodSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

*  ... use the handle ...

*  Close the handle to release the server-side resource.  A handle should not be used after it
*  is closed.
lnSuccess = loSftp.CloseHandle(lcHandle)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

? "Handle closed."

loSftp.Disconnect()

RELEASE loSftp