FTP file download using Delphi IdFTP

5.9k views Asked by At

I am new to delphi, I need to write an ftp client program that will go through a text file with list of ftp addresses, and download the subfolders from an ftp site . I have successfully hooked to the server but got stuck on the download part. can someone please help me with the codes to insert in the download procedure

 procedure TCleint.btnConnectClick(Sender: TObject);
begin
    try
       if not IdFTP.Connected then
       begin
           IdFTP.Host := 'ftp server';
           IdFTP.Username := 'anonymous';
           IdFTP.Password := 'emailaddress';
           IdFTP.Port := 21;
           IdFTP.Connect;

           IdFTP.List(listaDirectory.Items, '', false);

           btnConnect.Enabled := False;
           btnDisconnect.Enabled := True;
           btnDownload.Enabled := True;

       end;
    except
         on E:Exception do
         begin
             MessageDlg('connection error!', mtError, [mbOK], 0);
             btnConnect.Enabled := true;
             btnDisconnect.Enabled := false;
             btnDownload.Enabled := false;
         end;
    end;
end;

procedure TCleint.btnDisconnectClick(Sender: TObject);
begin
    try
       if IdFTP.Connected then
       begin
           IdFTP.Disconnect;

           listaDirectory.Clear;
           btnConnect.Enabled := True;
           btnDisconnect.Enabled := False;
           btnDownload.Enabled := False;
       end;
    except
        on E:Exception do
        begin
          MessageDlg('connection error!', mtError, [mbOK], 0);
          btnConnect.Enabled := false;
          btnDisconnect.Enabled := true;
          btnDownload.Enabled := true;
        end;
    end;
end;

procedure TCleint.btnDownloadClick(Sender: TObject);
begin

end;

end.
1

There are 1 answers

0
Remy Lebeau On

After calling List(), you need to loop through the entries of the DirectoryListing property. That will tell you which items are files and which are subfolders. You can then Get() the files and (recursively) ChangeDir()/List() the subfolders.