Sample code for 30+ languages & platforms
PureBasic 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 PureBasic Downloads

PureBasic
IncludeFile "CkSFtpFile.pb"
IncludeFile "CkSFtpDir.pb"
IncludeFile "CkSFtp.pb"

Procedure ChilkatExample()

    success.i = 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.

    sftp.i = CkSFtp::ckCreate()
    If sftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Set some timeouts, in milliseconds:
    CkSFtp::setCkConnectTimeoutMs(sftp, 5000)
    CkSFtp::setCkIdleTimeoutMs(sftp, 10000)

    ;  Connect to the SSH server.  
    ;  The standard SSH port = 22
    ;  The hostname may be a hostname or IP address.
    hostname.s = "www.my-sftp-server.com"
    port.i = 22
    success = CkSFtp::ckConnect(sftp,hostname,port)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  Authenticate with the SSH server.  Chilkat SFTP supports
    ;  both password-based authenication as well as public-key
    ;  authentication.  This example uses password authenication.
    success = CkSFtp::ckAuthenticatePw(sftp,"myLogin","myPassword")
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  After authenticating, the SFTP subsystem must be initialized:
    success = CkSFtp::ckInitializeSftp(sftp)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    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. 
    handle.s = CkSFtp::ckOpenDir(sftp,".")
    If CkSFtp::ckLastMethodSuccess(sftp) = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  Download the directory listing:

    dirListing.i = CkSFtpDir::ckCreate()
    If dirListing.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSFtp::ckReadDirListing(sftp,handle,dirListing)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        CkSFtpDir::ckDispose(dirListing)
        ProcedureReturn
    EndIf

    ;  Close the handle for the directory listing.
    success = CkSFtp::ckCloseHandle(sftp,handle)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        CkSFtpDir::ckDispose(dirListing)
        ProcedureReturn
    EndIf

    ;  Iterate over the files.
    fileObj.i = CkSFtpFile::ckCreate()
    If fileObj.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    n.i = CkSFtpDir::ckNumFilesAndDirs(dirListing)
    While i < n

        success = CkSFtpDir::ckFileAt(dirListing,i,fileObj)
        If success = 0
            Debug CkSFtpDir::ckLastErrorText(dirListing)
            CkSFtp::ckDispose(sftp)
            CkSFtpDir::ckDispose(dirListing)
            CkSFtpFile::ckDispose(fileObj)
            ProcedureReturn
        EndIf

        Debug CkSFtpFile::ckFilename(fileObj)
        Debug CkSFtpFile::ckFileType(fileObj)
        Debug "Size in bytes: " + Str(CkSFtpFile::ckSize32(fileObj))
        Debug "----"

        i = i + 1
    Wend

    Debug "Success."


    CkSFtp::ckDispose(sftp)
    CkSFtpDir::ckDispose(dirListing)
    CkSFtpFile::ckDispose(fileObj)


    ProcedureReturn
EndProcedure