每日程炼-IOUtils单元查找文件夹高级功能

  • A+
所属分类:[开发技巧]

IOUtils单元,查找文件夹内信息的getfile函数,还可以在内部嵌套函数,提升其功能的扩展,非常好用!

DEMO下载:IOUnit2

 

代码部分: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) mmo1: TMemo; btn1: TButton; btn2: TButton; btn3: TButton; btn4: TButton; procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); procedure btn3Click(Sender: TObject); procedure btn4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses IOUtils, Types; const path = 'D:\Program Files\Embarcadero\RAD Studio\7.0'; //准备给 TDirectory.GetFiles 调用的函数, 须符合 TDirectory.TFilterPredicate 格式 function fp(const Path: string; const SearchRec: TSearchRec): Boolean; begin Form1.mmo1.Lines.Add(Path + '\' + SearchRec.Name); end; procedure TForm1.btn1Click(Sender: TObject); var files: TStringDynArray; begin mmo1.Clear; files := TDirectory.GetFiles(path, fp); end; procedure TForm1.btn2Click(Sender: TObject); var files: TStringDynArray; begin mmo1.Clear; files := TDirectory.GetFiles(path,'*.txt', FP); end; procedure TForm1.btn3Click(Sender: TObject); var files: TStringDynArray; begin mmo1.Clear; files := TDirectory.GetFiles(path,'*.txt',TSearchOption.soAllDirectories,FP); end; procedure TForm1.btn4Click(Sender: TObject); var files: TStringDynArray; begin mmo1.Clear; files := TDirectory.GetFiles(path, function(const Path: string; const SearchRec: TSearchRec): Boolean begin mmo1.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 = 258 ClientWidth = 554 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object mmo1: TMemo Left = 8 Top = 0 Width = 545 Height = 177 ImeName = #20013#25991'('#31616#20307') - '#25628#29399#25340#38899#36755#20837#27861 Lines.Strings = ( 'mmo1') TabOrder = 0 end object btn1: TButton Left = 56 Top = 208 Width = 75 Height = 25 Caption = 'GetFiles' TabOrder = 1 OnClick = btn1Click end object btn2: TButton Left = 168 Top = 208 Width = 89 Height = 25 Caption = 'GetFiles+*.txt' TabOrder = 2 OnClick = btn2Click end object btn3: TButton Left = 288 Top = 208 Width = 130 Height = 25 Caption = 'GetFiles+*.txt+'#23376#30446#24405 TabOrder = 3 OnClick = btn3Click end object btn4: TButton Left = 432 Top = 208 Width = 81 Height = 25 Caption = 'GetFiles+'#20989#25968 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: