TIdFTP two important functions
Project development process found that for file / directory on the server directly process, is a very dangerous thing, because once the file / path does not exist, the program will throw an exception, affect the customer experience. Therefore, before the file / directory on the server is accessed, we generally first determine whether there is a path / file on the server, and then further processing.
Analyzing the server or the file path exists FtpDirectoryExists
Consider the following code:
1 function TMainFrm.FtpDirectoryExists(ADir: string): Boolean; 2 var 3 i, Index: Integer; 4 strlist: TStringList; 5 filename: string; 6 b: Boolean; 7 begin 8 Result := false; 9 Index := 0; 10 strlist := TStringList.Create; 11 strlist.Clear; 12 idFTP.List(strlist, '', True); 13 if strlist.Count > 0 then 14 begin 15 for i := 0 to strlist.Count - 1 do 16 begin 17 filename := trim(strlist.Strings[i]); 18 b := True; 19 while b do 20 begin 21 if Pos(' ', filename) > 0 then 22 begin 23 filename := Trim(Copy(filename, Pos(' ', filename), Length(filename))); 24 end; 25 if Pos(' ', filename) = 0 then b := False; 26 end; 27 if filename = ADir then 28 begin 29 Result := true; 30 Break; 31 end; 32 end; 33 end; 34 strlist.Free; 35 end;
Creating FTP path / folder CreateFtpDir
Consider the following code:
. 1 Procedure TMainFrm.CreateFtpDir (V_Path: String ); 2 var . 3 I, J, K: Integer; . 4 sFtpPath, S: TStringList; . 5 t_Path, SSTR: String ; . 6 the begin . 7 sFtpPath:. = TStringList the Create ; . 8 sFtpPath.Clear ; . 9 sFtpPath.Delimiter: = ' / ' ; 10 sFtpPath.DelimitedText: = V_Path; . 11 idFTP.ChangeDir ( ' / ' ;) 12 is // created automatically check whether the directory is present, there is no 13 for K := 0 to sFtpPath.Count - 1 do 14 begin 15 sSTR := sFtpPath[k]; 16 try 17 if not FtpDirectoryExists(sSTR) then 18 idFTP.MakeDir(sSTR); 19 except 20 on E: Exception do 21 begin 22 null; 23 end; 24 end; 25 idFTP.ChangeDir(sSTR + '/'); 26 end; 27 sFtpPath.Free; 28 end ;