Sample code for 30+ languages & platforms
Visual FoxPro

Open a Remote Directory on an SFTP Server

See more SFTP Examples

Demonstrates the Chilkat SFtp.OpenDir method, which opens a remote directory and returns a handle for reading its entries. The only argument is the directory path. Close the handle with CloseHandle when finished.

Background: Listing a directory in SFTP is a handle-based operation, mirroring file access: open the directory to get a handle, read its entries, then close it. The handle is passed to ReadDirListing to retrieve the names and attributes. As with file handles, the directory handle is a server-side resource that should be closed to avoid exhausting the server's limits.

Chilkat Visual FoxPro Downloads

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

lnSuccess = 0

*  Demonstrates the SFtp.OpenDir method, which opens a remote directory and returns a handle for
*  reading its entries.  The only argument is the directory path.

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 the remote directory.  OpenDir returns a handle string.
lcHandle = loSftp.OpenDir("subdir")
IF (loSftp.LastMethodSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

? "Opened directory, handle = " + lcHandle

*  The directory entries are read with ReadDirListing using this handle.

*  Close the handle when finished.
lnSuccess = loSftp.CloseHandle(lcHandle)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    CANCEL
ENDIF

loSftp.Disconnect()

RELEASE loSftp