Idftp遍历ftp上所有文件,针对网络上例子的修改。
网络中有很多篇关于“如何用idFTP遍历整个目录----下载、删除”的例子。但是这个例子有个致命缺陷,那就是当遇到文件名包含特殊字符(如#,&,$)就会造成程序死循环,我针对网上的例子进行了一点优化。并亲自测试通过,希望对广大网友有帮助。
procedure TMainForm.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 = 2 then exit;
for i:=0 to count1 - 1 do
begin //必须
IdFTP1.ChangeDir(serverPath);
IdFTP1.List(IdFTP1.ListResult);
FileName:=IdFTP1.DirectoryListing.Items[i].FileName;
if (filename='.') or (filename='..') then continue; //&&&&&&&&&关键就在这里
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;
//count1:=IdFTP1.DirectoryListing.Count;
end;
end;

浙公网安备 33010602011771号