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 已经最小化的窗体如何让它自己还原?

Delphi webbrowser中获取input表单值

Delphi判断字符串是否为数字

Delphi xe5 android应用程序获取电池信息

Delphi点击网页弹出的alert对话框的确定按钮

Delphi edit只能输入数字或小数点

Delphi的tfilestream 内存流

Delphi xe [dcc32 fatal error] f2039 could not create output file 问题的解决

Delphi 获取 treeview选中的文件路径

Delphi 获取本机 hostname ip address

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



打赏

取消

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

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

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

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

评论

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