还是关于获取远程FTP上的文件列表。老妖帮忙!

yykitty 2005-12-04 09:19:06
我使用了老妖的方法,只能获取文件夹里的文件,子文件夹里的文件获取不到,是不是要递归调用?拜托!
...全文
359 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
GWI2005 2005-12-06
  • 打赏
  • 举报
回复
up!,这个我也想知道!
constantine 2005-12-05
  • 打赏
  • 举报
回复
给你段delphi代码参考,是下载的,不过思想一样
标题: 可以下载整个目录的简单实现

function IsFileInUse(fName : string ):boolean; //检测文件是否正在使用
var
HFileRes : HFILE;
begin
Result := false;
if not FileExists(fName) then
exit;
HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;

procedure TDownForm.DownLoadDir(LocalPath,ServerPath:string);
var
i,count1:integer;
att:TIdDirItemType;
FileList : TStrings;
Name{, Line}: String;
ss:string;
begin
try begin
FileList := TStringList.Create;
ChageDir(serverpath);
Application.ProcessMessages;
if AnsiLastChar(serverpath) <> '/' then
serverpath := serverpath + '/';
if AnsiLastChar(localpath) <> '\' then
localpath := localpath + '\';
if not DirectoryExists(localpath+serverpath) then ForceDirectories(localpath+serverpath);
IdFTP1.List(FileList);
Application.ProcessMessages;
count1:=IdFTP1.DirectoryListing.Count;
for i:=0 to count1-1 do begin
ss:=IdFTP1.DirectoryListing.Items[i].FileName;
att:=IdFTP1.DirectoryListing.Items[i].ItemType;
if (att<>ditDirectory) then begin
if not DirectoryExists(localpath+serverpath) then ForceDirectories(localpath+serverpath);
BytesToTransfer := IdFTP1.Size(ss);
if IsFileInUse(ss)=False then
IdFTP1.Get(ss,localpath+serverpath+ss,true)
else Exit;
Application.ProcessMessages;
lbl1.Caption:='已下载:'+ss;
lbl1.Update;
end
end;
for i:=0 to count1-1 do begin
ss:=IdFTP1.DirectoryListing.Items[i].FileName;
att:=IdFTP1.DirectoryListing.Items[i].ItemType;
BytesToTransfer := IdFTP1.Size(ss);
if (att=ditDirectory) and (ss <> '.') AND (ss <> '..') then begin
Name := ss;
if not DirectoryExists(localpath+serverpath+Name) then ForceDirectories(localpath+serverpath+Name);
DownLoadDir(localpath+serverpath,Name);
Application.ProcessMessages
end;
end;
ChageDir('..');//这句是返回前一个目录,写的另一个函数,可以用IdFTP1.ChangeDir替换
Filelist.Free;
end
except
end;
end;

==============================================================================

procedure Tf_yrdwfileinfo.FTP_DownloadDir(remote_dir,local_dir:string); //下载整个目录,并遍历所有子目录 使用clftp
var
i,j,count1:integer;
att,ss:string;
current_dir,remote_dir2,currfile:string;
temp_dir,ftpdir:string;
F:textfile;
diratt:string;
filei:TFtpFileInfo;
lst : TStringList;
begin
ftpdir:=FtpClient1.HostDirName;
remote_dir2:=ftpdir+'/'+remote_dir;
FtpClient1.HostDirName :=remote_dir2;
//==========刷新目录
ftprefurbish(False );
//==========
current_dir:=remote_dir2; //主目录
temp_dir :=remote_dir ;
if not DirectoryExists(local_dir) then CreateDir(local_dir);
if not directoryexists(local_dir+'\'+temp_dir) then createdir(local_dir+'\'+temp_dir);

lst := TStringList.Create;
lst.LoadFromFile(TEMP_FILE_NAME);
for i :=2 to lst.Count - 1 do
begin
diratt:=lst[i];
filei:=GetFileInfo(diratt);
if (copy(filei.Attrib ,1,1)<>'d') then
begin
//if not DirectoryExists(local_dir) then CreateDir(local_dir); //创退本地目录
//==========下载文件
ftpclient1.Pwd ;
currfile:=ftpclient1.DirResult ;
currfile:=currfile+'/'+filei.FileName ;
ftpclient1.HostFileName :=currfile;
Ftpclient1.Binary :=true;
FtpClient1.TypeSet ;
ftpclient1.LocalFileName :=local_dir+'\'+temp_dir+'\'+filei.FileName ;
ftpclient1.Get ;
Ftpclient1.Binary :=false;
FtpClient1.TypeSet ;
//==========
end
else
begin
if not (filei.Filename ='..') then
begin
FTP_DownloadDir(filei.FileName,local_dir+'\'+remote_dir);
end;
end;
end;
lst.Free ;
end;
yykitty 2005-12-05
  • 打赏
  • 举报
回复
我用NMFTP->CurrentDir()获取到FTP上当前文件夹路径,郁闷的是,放在Edit里显示的是D:/文件夹/文件名,递归调用的文件路径应该是D:\文件夹\文件名,怎样把“/”改成“\”?
h2plus0 2005-12-04
  • 打赏
  • 举报
回复
自己写一个也不是很麻烦,具体参见《数据结构》
yykitty 2005-12-04
  • 打赏
  • 举报
回复
递归调用的代码我有,本地机的文件夹。使用NMFTP->Nlist()只能获取远程的文件名,取不到文件夹!

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧