{ 下载整个目录,并遍历所有子目录
首先 ChangeDir(Root) 到根目录
然后创建本地目录 + RemoteDir
然后用 list 得到所有目录名
循环判断,进入 RemoteDir 目录内部
如果是目录继续第归。否则 get 该文件到本地目录,当 get 完所有文件后返回上一级目录
用List再取得信息,继续循环
}
procedure FTP_DownloadDir(var idFTP : TIdFtp;RemoteDir,LocalDir : string);
label Files ;
var
i,DirCount : integer;
begin
if not DirectoryExists(LocalDir + RemoteDir) then
ForceDirectories(LocalDir + RemoteDir);
idFTP.ChangeDir(RemoteDir);
idFTP.List(nil);
DirCount := idFTP.DirectoryListing.Count ;
if DirCount = 0 then
begin
idFTP.ChangeDirUp;
idFTP.List(nil);
end;
for i := 0 to DirCount - 1 do
begin
if DirCount <> idFTP.DirectoryListing.Count then
begin
repeat
idFTP.ChangeDirUp;
idFTP.List(nil);
until DirCount = idFTP.DirectoryListing.Count ;
end;
if idFTP.DirectoryListing[i].ItemType = ditDirectory then
FTP_DownloadDir(idFTP,idFTP.DirectoryListing[i].FileName,LocalDir + RemoteDir + '\')
else begin
idFTP.Get(idFTP.DirectoryListing[i].FileName,LocalDir + RemoteDir + '\' +
idFTP.DirectoryListing[i].FileName,true);
//Form1.lb_num.Caption := IntToStr(StrToInt(Form1.lb_num.Caption) + 1);
//Form1.lb_num.Update;
if i = DirCount - 1 then
begin
idFTP.ChangeDirUp;
idFTP.List(nil);
end;
end;
end;
end;
procedure TForm1.Btt_DownLoadDirClick(Sender: TObject);
begin
IdFTP1.Connect(true,-1);
if IdFTP1.Connected then
begin
IdFTP1.ChangeDir('bigimage');
FTP_DownloadDir(IdFTP1,'1002.1002.1002','g:\ftpdir\');
end;
IdFTP1.Disconnect ;
end;
delphi中的nmftp控件中Download函数只能下载一个文件,没有提供一个下载整个目录(包含子目录)的函数。
我编写了个实现目录下载功能的方法,需要用到该功能的用户可参考一下。
file://目录下载
function tftp.ex_download(remote_dir,local_dir:string):boolean;
var
i,j,count1:integer;
att,ss:string;
current_dir:string;
temp_dir:string;
begin
try begin
NMFTP1.ChangeDir(remote_dir);
current_dir:=remote_dir;
temp_dir:=copy(current_dir,2,length(current_dir));
if not DirectoryExists(local_dir) then CreateDir(local_dir);
if not directoryexists(local_dir+temp_dir) then createdir(local_dir+temp_dir);
nmftp1.ParseList:=true;
NMftp1.list;
count1:=nmftp1.FTPDirectoryList.name.Count;
for i:=0 to count1-1 do begin
file://必须
NMFTP1.ChangeDir(current_dir);
nmftp1.list;
ss:=nmftp1.FTPDirectoryList.name.Strings[i];
att:=nmftp1.FTPDirectoryList.Attribute.Strings[i];
if (copy(pchar(att),1,1)<>'d')and(copy(pchar(att),1,1)<>'D') then begin
if not DirectoryExists(local_dir) then CreateDir(local_dir);
NMFTP1.Download(current_dir+ss,local_dir+temp_dir+ss);
end
else begin
if not directoryexists(local_dir+temp_dir+ss) then createdir(local_dir+temp_dir+ss);
file://递归调用
ex_download(remote_dir+ss+'\',local_dir);
end;
end;
result:=true;
end
except
On E:Exception do begin
result:=false;
end;
end;
end;