如何用idftp遍历整个目录—-下载、删除_delphi教程

如何用idftp遍历整个目录—-下载、删除_delphi教程如何用idftp遍历整个目录—-下载、删除_delphi教程unitUnit1;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,IdBaseComponent,IdComponent,IdTCPConn…

大家好,又见面了,我是你们的朋友全栈君。

如何用idftp遍历整个目录—-下载、删除_delphi教程

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.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150399.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 使用 Notifications

    使用 Notifications
    第十四章:使用Notifications
    用户可能使用RaiseMan并打开了几个document,然后他发现紫色的背景颜色实在是不利于阅读文档正文.于是,他打开Preferencespanel修改背景颜色,不过令人失望的是,已经存在的文档的背景颜色不会跟着改变.于是,这个用户可能会写信给你告诉你这些.你也许会回复:”defualts会在document创建的时候才读取,保存document在打开”实际上,用户想说明的是他希望程序能立马刷新已经打开的文档.如

    2022年10月29日
  • mybatis-log-plugin激活码(注册激活)2022.02.05

    (mybatis-log-plugin激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~CJM5ZJBPHS-eyJsaWNlbnNlSWQiOi…

    2022年4月1日
  • TCP bind() GetLastError() 10049[通俗易懂]

    TCP bind() GetLastError() 10049[通俗易懂]TCPbind()GetLastError()10049?试试将tcpServer.sin_addr.S_un.S_addr=inet_addr(Address().c_str())改成 tcpServer.sin_addr.S_un.S_addr=INADDR_ANY

    2022年10月1日
  • MFC 进度条使用方法[通俗易懂]

    MFC 进度条使用方法[通俗易懂]目的:学习MFC进度条控件的用法;步骤:新建一个对话框项目。添加控件“progress”“static”è改名了“进度”,添加两个BUTTON名字分别为“后退”“前进”,如下图:为static控件添加CString类型的数据变量m_present;为progress添加control类型的数据变量m_pro初始化进度条:右键classwinzerd,选中如下项目

    2022年7月27日
  • 频谱分析仪原理学习

    频谱分析仪原理学习 虽是电子专业出身,但在学生期间用频谱仪的次数比较少,连使用都不顺畅更加不会想到去研究它的原理。但现在的工作主要就是检测接收机,每天和频谱仪接收机各种设备打交道,有必要也很乐意的研究下各个设备的工作原理。讲解频谱仪原理的书籍有很多,读的第一本是师傅给我的安捷伦的《频谱分析原理》接着又自己看了《R&amp;S的频谱分析原理》,相较于安捷伦R&amp;S 更加注重从理论分析,个人…

    2022年8月11日
  • 如何删除远程连接记录

    如何删除远程连接记录

    2021年11月17日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号