Delphi TDirectory.GetDirectories获取子目录及文件


本文整理自网络,侵删。

 和 TDirectory.GetFiles 用法一样, TDirectory.GetDirectories 是用来获取子目录的.
另外还有 TDirectory.GetFileSystemEntries 可同时获取文件与子目录, 用法都一样.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  end;


var
  Form1: TForm1;


implementation


{$R *.dfm}


uses IOUtils, Types;

const path = 'C:\Program Files\Embarcadero\RAD Studio\7.0';

//获取指定目录下的文件与子目录
procedure TForm1.Button1Click(Sender: TObject);
var
  dfs: TStringDynArray;
  str: string;
begin
  dfs := TDirectory.GetFileSystemEntries(path);
  Memo1.Clear;
  for str in dfs do Memo1.Lines.Add(str);
end;


//获取指定目录下的、名称是 L 开头的文件与子目录
procedure TForm1.Button2Click(Sender: TObject);
var
  dfs: TStringDynArray;
  str: string;
begin
  dfs := TDirectory.GetFileSystemEntries(path, 'L*');
  Memo1.Clear;
  for str in dfs do Memo1.Lines.Add(str);
end;


//获取指定目录及嵌套目录下的所有文件与子目录
procedure TForm1.Button3Click(Sender: TObject);
var
  dfs: TStringDynArray;
  str: string;
begin
  dfs := TDirectory.GetFileSystemEntries(path + '\source', TSearchOption.soAllDirectories, nil);
  Memo1.Clear;
  for str in dfs do Memo1.Lines.Add(str);
end;


//使用 TDirectory.TFilterPredicate 函数参数
procedure TForm1.Button4Click(Sender: TObject);
var
  dfs: TStringDynArray;
begin
  Memo1.Clear;
  dfs := TDirectory.GetFileSystemEntries(path,
    function(const Path: string; const SearchRec: TSearchRec): Boolean
    begin
      //这里可以有更多过滤条件或其他处理
      Memo1.Lines.Add(Path + '\' + SearchRec.Name);
    end
  );
end;


end.

相关阅读 >>

Delphi ioutils 单元(6): tpath(结构体)路径的提取和处理

Delphi trimleft 删除字符串左边的空格

Delphi 中使长循环有响应

Delphi对access文件加密

Delphi 进程保护

Delphi 使电脑睡眠代码

Delphi tstopwatch 计时

Delphi 多桌面切换

Delphi xe android]获取屏幕的物理分辨率

Delphi 获取指定当前目录下指定文件扩展名所有文件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...