• 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;

  • 相关阅读:
    Jenkins的安全控制
    浅谈JNDI的使用
    hdu 4544 湫湫系列故事——消灭兔子
    如何在工作和实践中学习射频知识
    JSONObject 解析
    SharePoint 2010 WebPart Web部分 总的膏
    J2EE学习笔记
    C和指针 (pointers on C)——第十二章:利用结构和指针
    UVA
    EasyUI-DataGrid多线动态实现选择性合并
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/2266451.html
Copyright © 2020-2023  润新知