王朝网络
分享
 
 
 

如何用idFTP遍历整个目录----下载、删除

王朝delphi·作者佚名  2006-01-09
宽屏版  字体: |||超大  

如何用idFTP遍历整个目录—下载、删除

好久不在网上发表文章了,主要因为水平太臭,恐怕耽误了各位兄弟姐妹的前程,哈哈!

废话少说,下面切入正题。

这两天做一个项目,其中需要用ftp下载服务器上的整个目录,并且下载完成后,删除整个目录。由于ftp不能穿透子目录,只能在当前目录下操作,所以用一般的方法根本无法达到预期效果。可能我想偷懒吧!于是想从网上搜搜,看有没有现成的东东拿来使用 :)

结果令我非常失望,不是无法运行就是达不到我的预期效果。其实论坛上也有人问过这样的问题,可一直也没有满意的结果。哎!还得靠自己呀!小日本可没有那么听话,不知道大家听没听说钓鱼岛,去没去参加游行。

下面的程序是用delphi7.0 + idFTP 实现的。可能还会有bug,不过希望能给需要他的人带来一点点帮助和提示!,程序中只有下载与删除的代码,至于上传的code自己写吧,稍微思考一下就可以实现。

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,IdFTPList,

IdTCPClient, IdFTP ;

type

TForm1 = class(TForm)

Btt_DownLoadDir: TButton;

IdFTP1: TIdFTP;

Btt_DeleteDir: TButton;

Label1: TLabel;

lb_num: TLabel; //处理文件个数提示。

procedure Btt_DownLoadDirClick(Sender: TObject);

procedure Btt_DeleteDirClick(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

{ 下载整个目录,并遍历所有子目录

首先 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;

{删除整个ftp目录,包括下面的文件,

RootDir = 要删除的根目录,一般情况下 RemoteDir 与 RootDir 相等}

procedure FTP_DeleteAllFiles(var idFTP : TIdFtp;RemoteDir,RootDir : string);

label Files;

var

i,DirCount : integer;

Temp : string;

begin

idFTP.ChangeDir(RemoteDir);

if Pos(RootDir,idFTP.RetrieveCurrentDir) = 0 then Exit;

Files :

idFTP.List(nil);

DirCount := idFTP.DirectoryListing.Count ;

while DirCount = 0 do

begin

Temp := idFTP.RetrieveCurrentDir;

idFTP.ChangeDirUp;

idFTP.RemoveDir(Temp);

idFTP.List(nil);

DirCount := idFTP.DirectoryListing.Count ;

for i := 0 to DirCount - 1 do

if idFTP.DirectoryListing[i].FileName = RootDir then Exit;

end;

for i := 0 to DirCount - 1 do

begin

if Pos(RootDir,idFTP.RetrieveCurrentDir) = 0 then Break ;

if idFTP.DirectoryListing[i].ItemType = ditDirectory then

begin

FTP_DeleteAllFiles(idFTP,idFTP.DirectoryListing[i].FileName,RootDir);

end else begin

idFTP.Delete(idFTP.DirectoryListing[i].FileName);

Form1.lb_num.Caption := IntToStr(StrToInt(Form1.lb_num.Caption) + 1);

Form1.lb_num.Update;

goto Files ;

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;

procedure TForm1.Btt_DeleteDirClick(Sender: TObject);

begin

IdFTP1.Connect(true,-1);

if IdFTP1.Connected then

begin

IdFTP1.ChangeDir('bigimage');

FTP_DeleteAllFiles(IdFTP1,'1002.1002.1002','1002.1002.1002');

end;

IdFTP1.Disconnect ;

end;

end.

运行环境 win2000 advanced server + delphi7.0 + iis6.0

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有