Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

SFTP Read Directory Listing

See more SFTP Examples

Demonstrates how to download a directory listing and iterate over the files.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSftp
LOCAL cHostname
LOCAL nPort
LOCAL cHandle
LOCAL oDirListing
LOCAL oFileObj
LOCAL i
LOCAL n

nSuccess := 0

//  This requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Important: It is helpful to send the contents of the
//  sftp.LastErrorText property when requesting support.

oSftp := CreateObject("Chilkat.SFtp")

//  Set some timeouts, in milliseconds:
oSftp:ConnectTimeoutMs := 5000
oSftp:IdleTimeoutMs := 10000

//  Connect to the SSH server.  
//  The standard SSH port = 22
//  The hostname may be a hostname or IP address.
cHostname := "www.my-sftp-server.com"
nPort := 22
nSuccess := oSftp:Connect(cHostname, nPort)
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Authenticate with the SSH server.  Chilkat SFTP supports
//  both password-based authenication as well as public-key
//  authentication.  This example uses password authenication.
nSuccess := oSftp:AuthenticatePw("myLogin", "myPassword")
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  After authenticating, the SFTP subsystem must be initialized:
nSuccess := oSftp:InitializeSftp()
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Open a directory on the server...
//  Paths starting with a slash are "absolute", and are relative 
//  to the root of the file system. Names starting with any other 
//  character are relative to the user's default directory (home directory).
//  A path component of ".." refers to the parent directory, 
//  and "." refers to the current directory. 
cHandle := oSftp:OpenDir(".")
IF (oSftp:LastMethodSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Download the directory listing:

oDirListing := CreateObject("Chilkat.SFtpDir")
nSuccess := oSftp:ReadDirListing(cHandle, oDirListing)
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    oDirListing:destroy()
    RETURN
ENDIF

//  Close the handle for the directory listing.
nSuccess := oSftp:CloseHandle(cHandle)
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    oDirListing:destroy()
    RETURN
ENDIF

//  Iterate over the files.
oFileObj := CreateObject("Chilkat.SFtpFile")
i := 0
n := oDirListing:NumFilesAndDirs
DO WHILE i < n

    nSuccess := oDirListing:FileAt(i, oFileObj)
    IF (nSuccess == 0)
        ? oDirListing:LastErrorText
        oSftp:destroy()
        oDirListing:destroy()
        oFileObj:destroy()
        RETURN
    ENDIF

    ? oFileObj:Filename
    ? oFileObj:FileType
    ? "Size in bytes: " + Str(oFileObj:Size32)
    ? "----"

    i := i + 1
ENDDO

? "Success."

oSftp:destroy()
oDirListing:destroy()
oFileObj:destroy()