Xceed FTP Library Documentation
Listing a remote folder's contents (Delphi example)
Example topics > Listing a remote folder's contents (Delphi example)

This example demonstrates the 8 following steps for listing files with the ListFolderContents method: 

  1. Specify the FTP server's address 
  2. Specify the username to login 
  3. Specify the password for the username 
  4. Connect to the FTP server 
  5. Begin the listing process 
  6. Process each item being listed 
  7. Disconnect from the FTP server 
  8. Perform error handling  

The following code assumes you have placed an XceedFtp control and a Listbox on a form and have called them 'XceedFtp1' and 'Listbox1' respectively (the default names).

The first section of code below demonstrates all steps except step (6). Step (6), the handler for the ListingFolderItem event, is found right after.  

Delphi Copy Code

begin
   XceedFtp1.Licenser( 'your license key' );

   XceedFtp1.ServerAddress := 'ftp.cdrom.com'; {step (1)}
   XceedFtp1.UserName := 'anonymous'; {step (2)}
   XceedFtp1.Password := 'guest'; {step (3)}

   try {step (8)}
      XceedFtp1.Connect(); {step (4)}
      ShowMessage('Successfully connected to server. Starting the listing of the contents of the current folder.');

      try {step (8)}
         XceedFtp1.ListFolderContents(''); {step (5)}
      except
         on xErr: Exception do
            ShowMessage('Error while listing. Description: ' + xErr.Message)
      end;

      try {step (8)}
         XceedFtp1.Disconnect(); {step (7)}
      except
         on xErr: Exception do
            ShowMessage('Error while disconnecting. Description: ' + xErr.Message)
      end;

      except
         on xErr: Exception do
            ShowMessage('Error while connecting. Description: ' + xErr.Message)
      end;
   end;

{ Step (6): The following code which adds the name and size of each
item being listed into a listbox on your form) must go into the error
handler for the ListingFolderItem event.}

procedure TForm1.XceedFtp1ListingFolderItem(Sender: TObject; const sName: WideString; dtDate: TDateTime; lFileSize: Integer; eItemType: TOleEnum; const sUserData: WideString);
var
   sFileSize: String;
begin
   sFileSize := IntToStr(lFileSize);

   case eItemType of
      fitFile: ListBox1.Items.Add('File: ' + sName + ' (' + sFileSize + ' bytes)');
      fitFolder: ListBox1.Items.Add('Folder: ' + sName);
      fitLink: ListBox1.Items.Add('Link: ' + sName + ' (' + sFileSize + ' bytes)');
   end;
end;