IDFTP遍历服务器上的文件夹下载里边的内容

写了个从ftp上下载文件的程序;
一直无法成功执行;
每次都是下载一个文件夹后就结束;
出错提示是【list   index   out   of   index(数字)】

procedure   TFormMain.DownAllFile(Localpath,   serverPath:   string);
var
      i,count1:integer;
      ss:string;
      att:TIdDirItemType;
      FileList     :   TStrings;
begin
      try   begin
              FileList   :=   TStringList.Create;
              IdFTP1.ChangeDir(serverpath);
              if   AnsiLastChar(serverpath)   <>   '/ '   then
                  serverpath   :=   serverpath   +   '/ ';
              if   AnsiLastChar(localpath)   <>   '\ '   then
                  localpath   :=   localpath   +   '\ ';
              if   not   DirectoryExists(localpath)   then   CreateDir(localpath);
                IdFTP1.List(FileList);
              count1:=IdFTP1.DirectoryListing.Count;
              if   count1   =   0   then   exit;

              for   i:=0   to   count1-1     do
              begin
                  ss:=IdFTP1.DirectoryListing.Items[i].FileName;
                  att:=IdFTP1.DirectoryListing.Items[i].ItemType;
                  if   (att <> ditDirectory)   and   (ss   <>   '. ')   AND   (ss   <>   '.. ')   then
                  begin
                      if   not   DirectoryExists(localpath)   then   CreateDir(localpath);
                      IdFTP1.Get(serverpath+ss,localpath+ss,true);
                  end
              end;

              for   i:=0   to   count1-1     do
              begin
                          ss:=IdFTP1.DirectoryListing.Items[i].FileName;
                          att:=IdFTP1.DirectoryListing.Items[i].ItemType;
                      if   (att=ditDirectory)   and   (ss   <>   '. ')   AND   (ss   <>   '.. ')   then
                      begin
                          DownAllFile(localpath+ss,serverpath+ss);
                      end;
              end;
                IdFTP1.ChangeDirUp;
      end
      finally
          Filelist.Free;
      end;
end;

改正后

procedure   TFormMain.downloadAll(Localpath,   serverPath:   string);
var
      i,count1:integer;
      FileList     :   TStrings;
      FileName:   string;
      FilemodifyDate:   TDatetime;
begin
      try
              FileList   :=   TStringList.Create;
              IdFTP1.ChangeDir(serverpath);
              if   AnsiLastChar(serverpath)   <>   '/ '   then
                  serverpath   :=   serverpath   +   '/ ';
              if   AnsiLastChar(localpath)   <>   '\ '   then
                  localpath   :=   localpath   +   '\ ';
              if   not   DirectoryExists(localpath)   then   CreateDir(localpath);
                IdFTP1.List(IdFTP1.ListResult);

              count1:=IdFTP1.DirectoryListing.Count;
              if   count1   =   0   then   exit;

        for   i:=0   to   count1   -   1   do
        begin         //必须
            IdFTP1.ChangeDir(serverPath);
            IdFTP1.List(IdFTP1.ListResult);

            FileName:=IdFTP1.DirectoryListing.Items[i].FileName;
            FilemodifyDate   :=   IdFTP1.DirectoryListing.Items[i].ModifiedDate;
            if   IdFTP1.DirectoryListing.Items[i].ItemType   =   ditfile   then         //为文件
            begin
                IdFTP1.Get(FileName,Localpath   +   FileName,true);
            end
            else   if   IdFTP1.DirectoryListing.Items[i].ItemType   =   ditdirectory   then                   //为文件夹
            begin
            if   not   directoryexists(Localpath   +   filename   +   '\ ')   then
                createdir(Localpath   +   filename   +   '\ ');
                //递归调用
                downloadAll(Localpath   +   filename   +   '\ ',   serverPath+ '/ '+FileName);
            end;
        end;
    finally
        IDFtp1.ChangeDirUp;
    end;
end;

原文地址:https://www.cnblogs.com/zhangzhifeng/p/2266451.html