ICS FTP - 用于检查ftp服务器上是否存在文件夹的功能

时间:2016-01-03 14:38:37

标签: delphi ftp

我正在尝试使用Overbyte ICS FTP组件创建一个检查文件夹是否存在的函数。使用icsftp中的DIR命令不会在我的备忘录日志中显示任何内容。 我有兴趣将dir命令的结果解析为字符串列表,以便搜索特定的文件夹。

目前我使用像这样的indy函数。我如何用ICS做同样的事情?

function exista_textul_in_stringlist(const stringul_pe_care_il_caut:string; stringlistul_in_care_efectuez_cautarea:Tstringlist):boolean;

begin
if stringlistul_in_care_efectuez_cautarea.IndexOf(stringul_pe_care_il_caut) = -1 then
begin
  result:=false;
  //showmessage('Textul "'+text+'" nu exista!' );
end
else
begin

 result:=true;
 //showmessage('Textul "'+text+'" exista la pozitia '+ inttostr(ListBox.Items.IndexOf(text)));
end;
end;

    function folder_exists_in_ftp(folder_name_to_search_for,ftp_hostname,ftp_port,ftp_username,ftp_password,ftp_root_folder:string;memo_loguri:Tmemo):boolean;
    Var
     DirList : TStringList;
     ftp:Tidftp;
     antifreeze:TidAntifreeze;
     var i,k:integer;
    begin
     dateseparator:='-';
     Result := False;
     DirList := TStringList.Create;
     ftp:=tidftp.Create;
     antifreeze:=TidAntifreeze.Create;
     try
        antifreeze.Active:=true;
        ftp.Host:=ftp_hostname;
        ftp.Port:=strtoint(ftp_port);
        ftp.username:=ftp_username;
        ftp.password:=ftp_password;
        ftp.Passive:=true;
        ftp.Connect;

     ftp.ChangeDir(ftp_root_folder);
     ftp.List(DirList, folder_name_to_search_for, True);


      if DirList.Count > 0 then begin
          k := DirList.Count;
          DirList.Clear; // DIRLIST will hold folders only
          for i := 0 to k - 1 do begin
            if (ftp.DirectoryListing.Items[i].FileName <> '.') and (ftp.DirectoryListing.Items[i].FileName <> '..') then begin
              if ftp.DirectoryListing.Items[i].ItemType = ditDirectory then begin
                DirList.Add(ftp.DirectoryListing.Items[i].FileName);
              end;
            end;
          end;
      end;
        if exista_textul_in_stringlist(folder_name_to_search_for,DIRLIST) then
      begin
      Result := True;
      memo_loguri.Lines.Add(datetimetostr(now)+' - caut folderul "'+folder_name_to_search_for+'" in directorul ftp "'+ftp_root_folder+'" => EXISTS!');
      end

      ELSE
      begin
      result:=false;
      memo_loguri.Lines.Add(datetimetostr(now)+' - caut folderul "'+folder_name_to_search_for+'" in directorul ftp "'+ftp_root_folder+'" => NOT exists!');
      end;
     finally
      ftp.Free;
      antifreeze.Free;
      DirList.Free;
     end;

    end;

2 个答案:

答案 0 :(得分:1)

您最好使用SIZETFtpClient.Size)或MLSTTFtpClient.Mlst)之类的命令来检查文件是否存在。

使用LIST是一种过度杀伤力。

答案 1 :(得分:1)

我假设您使用的是OverbyteIcs (ICS-V8.16 (Apr, 2015))的最新发布版本。

如果您只是需要检查一个远程目录是否存在,那么在另一个答案中提到的一个好建议是避免列表(如果返回了大量文件和文件夹,这可能是一个耗时的操作)。

我建议您尝试“乐观”并使用FTP.Cwd更改为您要调查的远程目录。如果此调用返回true,则文件夹当然存在,如果您计划继续使用同一客户端,则必须更改回原始目录。另一方面,如果调用失败,如果ftp服务器响应代码550,则该目录不存在。

我已经包含了一个执行上述操作的简单示例(但是,它没有提供“更改回原始dir-on-success”功能):

uses
  ...
  OverbyteIcsFtpCli;

function FtpRemoteDirExists( 
                           HostName: String; 
                           UserName: String; 
                           Password: String; 
                           HostDirToCheck : String ) : Boolean;
const
  cFtpCode_FileOrDirNotExists = 550;
var
  FTP: TFtpClient;
begin
  FTP := TFtpClient.Create(nil);
  try
    FTP.HostName := HostName;
    FTP.Passive := True;
    FTP.Binary := True;
    FTP.Username := UserName;
    FTP.Password := Password;
    FTP.Port := '21';

    if not FTP.Open then
      raise Exception.Create('Failed to connect: ' + FTP.ErrorMessage);

    if (not FTP.User) or (not FTP.Pass) then
      raise Exception.Create('Failed to login: ' + FTP.ErrorMessage);

    FTP.HostDirName := HostDirToCheck;
    if FTP.Cwd then
      Result := True
    else
    begin
      if FTP.StatusCode = cFtpCode_FileOrDirNotExists then
        Result := False
      else
        raise Exception.Create('Failed to change dir: ' + FTP.ErrorMessage);
    end;

  finally
    FTP.Free;
  end;
end;
相关问题