每日程炼-IOUtils 单元(4): TDirectory.GetDirectories获取子目录

  • A+
所属分类:[实用软件]

和 TDirectory.GetFiles 用法一样,

TDirectory.GetDirectories 是用来获取子目录的.

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

Demo下载:IOUnit4

和 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. 窗体部分: object Form1: TForm1 Left = 0 Top = 0 Caption = #24858#20154#31508#35760'-http://www.yrnote.com' ClientHeight = 327 ClientWidth = 388 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object Memo1: TMemo Left = 0 Top = 0 Width = 377 Height = 225 ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861 Lines.Strings = ( 'Memo1') TabOrder = 0 end object btn1: TButton Left = 8 Top = 248 Width = 145 Height = 25 Caption = 'GetFileSystemEntries' TabOrder = 1 OnClick = btn1Click end object btn2: TButton Left = 8 Top = 294 Width = 217 Height = 25 Caption = 'GetFileSystemEntries+soAllDirectories' TabOrder = 2 OnClick = btn2Click end object btn3: TButton Left = 192 Top = 248 Width = 169 Height = 25 Caption = 'GetFileSystemEntries+L*' TabOrder = 3 OnClick = btn3Click end object btn4: TButton Left = 248 Top = 294 Width = 129 Height = 25 Caption = 'SearchRec+function' TabOrder = 4 OnClick = btn4Click end end

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
广告也精彩
avatar
广告也精彩

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: