Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
sftp: HCkSFtp;
port: Integer;
password: PWideChar;
handle: PWideChar;
dirListing: HCkSFtpDir;
n: Integer;
fileObj: HCkSFtpFile;
i: Integer;

begin
success := False;

//  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 := CkSFtp_Create();

//  Connect, authenticate, and initialize the SFTP subsystem.
port := 22;
success := CkSFtp_Connect(sftp,'sftp.example.com',port);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
    Exit;
  end;

//  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 := 'mySshPassword';

success := CkSFtp_AuthenticatePw(sftp,'mySshLogin',password);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
    Exit;
  end;

success := CkSFtp_InitializeSftp(sftp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
    Exit;
  end;

//  Open the remote directory to be listed.  OpenDir returns a handle string.
handle := CkSFtp__openDir(sftp,'subdir');
if (CkSFtp_getLastMethodSuccess(sftp) = False) then
  begin
    Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
    Exit;
  end;

//  Read all directory entries into an SFtpDir object.
dirListing := CkSFtpDir_Create();
success := CkSFtp_ReadDirListing(sftp,handle,dirListing);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
    Exit;
  end;

//  The listing has been read, so the directory handle can be closed now.
success := CkSFtp_CloseHandle(sftp,handle);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
    Exit;
  end;

//  Iterate the entries.  Entries are in server order (Chilkat does not sort the listing).
n := CkSFtpDir_getNumFilesAndDirs(dirListing);
Memo1.Lines.Add('Directory contains ' + IntToStr(n) + ' entries:');

fileObj := CkSFtpFile_Create();

for i := 0 to n - 1 do
  begin
    //  FileAt fills the SFtpFile object with the entry at the given index.
    success := CkSFtpDir_FileAt(dirListing,i,fileObj);
    if (success = False) then
      begin
        Memo1.Lines.Add(CkSFtpDir__lastErrorText(dirListing));
        Exit;
      end;

    if (CkSFtpFile_getIsDirectory(fileObj)) then
      begin
        Memo1.Lines.Add('  [DIR]  ' + CkSFtpFile__filename(fileObj));
      end
    else
      begin
        //  Size64 is a 64-bit size; LastModifiedTimeStr is the modification time as text.
        Memo1.Lines.Add('  ' + CkSFtpFile__filename(fileObj) + '  (' + IntToStr(CkSFtpFile_getSize64(fileObj)) + ' bytes, modified ' + CkSFtpFile__lastModifiedTimeStr(fileObj)
             + ')');
      end;
  end;

CkSFtp_Disconnect(sftp);

CkSFtp_Dispose(sftp);
CkSFtpDir_Dispose(dirListing);
CkSFtpFile_Dispose(fileObj);