How to delete directories containing subdirectories and files on an FTP server in Delphi?

Asked

Viewed 641 times

-1

I am for some time trying to make my application made in Delphi 2010 remove a directory on an FTP server, I used Tidftp for such a task with the command "Remove", but the process fails, because according to the application the directory is not empty. What can I do to solve my problem?

1 answer

1


As far as I know, Indy doesn’t have a "ready" method for what he wants. You will need to create a recursive method to delete all files and subdirectories before removing the root directory. See this example below adapted from a code obtained on the site progtown.com :

procedure FTPRemoveDir(Dir: string);
var
  I: Integer;
  List: TStringList;
begin
  List := TStringList.Create;
  try
    FTP.ChangeDir(Dir);
    FTP.List(List, '', false);
    for i := 0 to List.Count - 1 do
    begin
      if FTP.Size(List[I]) = -1 then
        DelFTPDir(List[I])
      else
        FTP.Delete(List[I]);
    end;
    FTP.ChangeDirUp;
    FTP.RemoveDir(Dir);
  finally
    List.Free;
  end;
end;

I hope I helped! Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.