Sample code for 30+ languages & platforms
Visual FoxPro

List a Remote Directory on an SFTP Server

See more SFTP Examples

Demonstrates the Chilkat SFtp.ReadDirListing method, which reads all remaining entries from an open directory handle into an SFtpDir object. The first argument is the handle returned by OpenDir and the second is the SFtpDir that receives the listing. The example iterates the entries with NumFilesAndDirs and FileAt, reading each SFtpFile's Filename, IsDirectory, Size64, and LastModifiedTimeStr.

Background: Listing a directory is a three-part operation: OpenDir gets a handle, ReadDirListing pulls every entry into an SFtpDir in one call, and the handle is then closed. Each entry is an SFtpFile carrying that item's name, type, size, timestamps, owner, and permission flags — a single stat covering what several separate GetFile* calls would return. The entries come back in the server's own order rather than sorted; use SFtpDir.Sort if you need a particular ordering. Note that a listing normally includes the . and .. entries, which most code skips.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSftp
LOCAL lnPort
LOCAL lcPassword
LOCAL lcHandle
LOCAL loDirListing
LOCAL n
LOCAL loFileObj
LOCAL i

lnSuccess = 0

*  Demonstrates the SFtp.ReadDirListing method, which reads all remaining entries from an open
*  directory handle into an SFtpDir object.  The 1st argument is the handle (from OpenDir) and
*  the 2nd is the SFtpDir that receives the listing.

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

*  Read all directory entries into an SFtpDir object.
loDirListing = CreateObject('Chilkat.SFtpDir')
lnSuccess = loSftp.ReadDirListing(lcHandle,loDirListing)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    RELEASE loDirListing
    CANCEL
ENDIF

*  The listing has been read, so the directory handle can be closed now.
lnSuccess = loSftp.CloseHandle(lcHandle)
IF (lnSuccess = 0) THEN
    ? loSftp.LastErrorText
    RELEASE loSftp
    RELEASE loDirListing
    CANCEL
ENDIF

*  Iterate the entries.  Entries are in server order (Chilkat does not sort the listing).
n = loDirListing.NumFilesAndDirs
? "Directory contains " + STR(n) + " entries:"

loFileObj = CreateObject('Chilkat.SFtpFile')

FOR i = 0 TO n - 1
    *  FileAt fills the SFtpFile object with the entry at the given index.
    lnSuccess = loDirListing.FileAt(i,loFileObj)
    IF (lnSuccess = 0) THEN
        ? loDirListing.LastErrorText
        RELEASE loSftp
        RELEASE loDirListing
        RELEASE loFileObj
        CANCEL
    ENDIF

    IF (loFileObj.IsDirectory) THEN
        ? "  [DIR]  " + loFileObj.Filename
    ELSE
        *  Size64 is a 64-bit size; LastModifiedTimeStr is the modification time as text.
        ? "  " + loFileObj.Filename + "  (" + STR(loFileObj.Size64) + " bytes, modified " + loFileObj.LastModifiedTimeStr + ")"
    ENDIF

NEXT

loSftp.Disconnect()

RELEASE loSftp
RELEASE loDirListing
RELEASE loFileObj