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

简介: 如何用idftp遍历整个目录----下载、删除_delphi教程 unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, ...

如何用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.

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
4月前
|
vr&ar 开发工具 iOS开发
visionOS空间计算实战开发教程Day 1:环境安装和编写第一个程序
截至目前visionOS还未在Xcode稳定版中开放,所以需要下载Xcode Beta版。比如我们可以下载Xcode 15.1 beta 2,注意Xcode 15要求系统的版本是macOS Ventura 13.5或更新,也就是说2017年的MacBook Pro基本可以勉强一战,基本上还是推荐使用M系列芯片的电脑进行开发。
67 0
|
6月前
|
Unix Windows
Notepad++简单使用(删除一行数据)
Notepad++简单使用(删除一行数据)
94 1
WIN10文件名排序错误的案例截图
WIN10文件名排序错误的案例截图
67 0
WIN10文件名排序错误的案例截图
|
JSON 自然语言处理 数据格式
插件安装演示Ⅱ | 学习笔记
快速学习插件安装演示Ⅱ
65 0
插件安装演示Ⅱ | 学习笔记
|
小程序 Python
10 行 Python 代码实现,电脑自动清理电脑内重复文件
给定一个文件夹,使用 Python 检查给定文件夹下有无文件重复,若存在重复则删除。 主要涉及的知识点有: os 模块综合应用 glob 模块综合应用 利用 filecmp 模块比较两个文件
146 0
10 行 Python 代码实现,电脑自动清理电脑内重复文件
|
测试技术 C语言
【超详解加源代码和测试截图】顺序表的一系列操作合集
动态顺序表建立和初始化, 检查容量和扩容,尾插和尾删,头插和头删.....
69 0
【超详解加源代码和测试截图】顺序表的一系列操作合集
|
前端开发
【前端】工具的下载使用+第一个简单的程序
【前端】工具的下载使用+第一个简单的程序
学习笔记(01):【吴刚】PS软件基础实用技巧标准视频教程-04-文件的打开及注意事项...
学习笔记(01):【吴刚】PS软件基础实用技巧标准视频教程-04-文件的打开及注意事项...
98 0