Xceed FTP Library Documentation
Example topics / Getting a remote folder's contents (Delphi example)
In This Topic
    Getting a remote folder's contents (Delphi example)
    In This Topic

    This example demonstrates the 8 following steps for listing files with the GetFolderContents 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. Obtain the directory listing object 
    6. Disconnect from the FTP server 
    7. Process the listed items 
    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) 

    Delphi Copy Code

    var   xFolderItems : XceedFtpFolderItems; {for step (5)} 
       xItem : XceedFtpFolderItem; {for step (6)} 
       nItemIndex : integer; {for step (6)} 
       vaIndex : OleVariant; {for step (6)} 
    begin
       XceedFtp1.License( '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. Obtaining folder contents.');  

          try {step (8)}  

             xFolderItems := XceedFtp1.GetFolderContents('',fcfCollection) As IXceedFtpFolderItems; {step (5)}

             ShowMessage('Successfully obtained the contents of the current remote folder');  

             XceedFtp1.Disconnect(); {step (6)}  

             nItemIndex := 1; {step (7)}  

             while nItemIndex <= xFolderItems.Count do {step (7)} 
             begin 
                vaIndex := nItemIndex; 
                xItem := xFolderItems.Item[ vaIndex ]; {step (7)} 

                ListBox1.Items.Add(xItem.ItemName + ' -> Size : ' + IntToStr(xItem.FileSize)); 

                nItemIndex := nItemIndex + 1; 
             end; 
          except {step (8)} 
             on xErr: Exception do 
                ShowMessage('Error while getting folder contents. Description: ' +
                            xErr.Message) 
          end; 
       except 
          on xErr: Exception do 
             ShowMessage('Error while connecting. Description: ' + xErr.Message) 
       end; 
    end;