Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

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

Procedure ChilkatExample()

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

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

    ;  Connect, authenticate, and initialize the SFTP subsystem.
    port.i = 22
    success = CkSFtp::ckConnect(sftp,"sftp.example.com",port)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    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.
    password.s = "mySshPassword"

    success = CkSFtp::ckAuthenticatePw(sftp,"mySshLogin",password)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    success = CkSFtp::ckInitializeSftp(sftp)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  Open the remote directory to be listed.  OpenDir returns a handle string.
    handle.s = CkSFtp::ckOpenDir(sftp,"subdir")
    If CkSFtp::ckLastMethodSuccess(sftp) = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  Read all directory entries into an SFtpDir object.
    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

    ;  The listing has been read, so the directory handle can be closed now.
    success = CkSFtp::ckCloseHandle(sftp,handle)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        CkSFtpDir::ckDispose(dirListing)
        ProcedureReturn
    EndIf

    ;  Iterate the entries.  Entries are in server order (Chilkat does not sort the listing).
    n.i = CkSFtpDir::ckNumFilesAndDirs(dirListing)
    Debug "Directory contains " + Str(n) + " entries:"

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

    i.i
    For i = 0 To n - 1
        ;  FileAt fills the SFtpFile object with the entry at the given index.
        success = CkSFtpDir::ckFileAt(dirListing,i,fileObj)
        If success = 0
            Debug CkSFtpDir::ckLastErrorText(dirListing)
            CkSFtp::ckDispose(sftp)
            CkSFtpDir::ckDispose(dirListing)
            CkSFtpFile::ckDispose(fileObj)
            ProcedureReturn
        EndIf

        If CkSFtpFile::ckIsDirectory(fileObj)
            Debug "  [DIR]  " + CkSFtpFile::ckFilename(fileObj)
        Else
            ;  Size64 is a 64-bit size; LastModifiedTimeStr is the modification time as text.
            Debug "  " + CkSFtpFile::ckFilename(fileObj) + "  (" + Str(CkSFtpFile::ckSize64(fileObj)) + " bytes, modified " + CkSFtpFile::ckLastModifiedTimeStr(fileObj) + ")"
        EndIf

    Next

    CkSFtp::ckDisconnect(sftp)


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


    ProcedureReturn
EndProcedure