lichengcom2009 发表于 2015-11-6 10:09:30

用idftp控件查询ftp服务器文件目录是否存在

  判断文件目录名称是否存在
  function TUPfileFRM.FtpDirectoryExists(ADir: string): Boolean;
var
index:Integer;
begin
Index:=0;
   Result := false;
   try
   if Assigned(dllUPfileFRM.IdFtp1.DirectoryListing) and (dllUPfileFRM.IdFtp1.DirectoryListing.Count>0) then
       while Index<dllUPfileFRM.IdFtp1.DirectoryListing.Count do
       begin
       with dllUPfileFRM.IdFtp1.DirectoryListing.Items do
       begin
         if (trim(FileName)=trim(ADir)) and (ItemType = ditDirectory)then    //// 是文件夹类型 编译不通过时要USES IDFTPLIST
         begin
         Result:=true;
         Exit;
         end;
       end;
       Index:=Index&#43;1;
       end;
   except
   Result := False;
   end;
end;
  
  调用判断函数的功能:
  functionTUPfileFRM.Dllupfiles(ASourceFile,ADestFile,sAir:string):boolean;
  var
LS: TStringList;
begin
result:=false;
LS:=TStringList.Create;
try
    IdFTP1.Host := '192.168.0.1';
    IdFTP1.Username := 'username';
    IdFTP1.Password := 'pass123';
    IdFTP1.TransferType := ftASCII;                            //// 编译不通过时 USES IdFTPCommon
    if IdFTP1.Connected then IdFTP1.Disconnect;
    IdFTP1.Connect();
    IdFTP1.List(LS);
    if (not FtpDirectoryExists(sAir)) thenIdFTP1.MakeDir(sAir);
  try
      IdFTP1.ChangeDirUp;
      IdFTP1.Put (ASourceFile,ADestFile);
      showmessage('上传文件成功!');
      result:=true;
    except
      showmessage('上传文件失败');
    end;
    finally
      LS.Free;
   end;
end;
  
         版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: 用idftp控件查询ftp服务器文件目录是否存在