Delphi xe IOUtils 单元(1): 初识 TDirectory.GetFiles


本文整理自网络,侵删。

 
用 IOUtils 单元下的 TDirectory.GetFiles 获取文件列表太方便了;

下面的例子只是 TDirectory.GetFiles 的典型应用...

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;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(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
  dir: TDirectory; {这是个结构, 后面就不再声明了, 将直接使用}
  files: TStringDynArray; {TStringDynArray = array of string;}
  str: string;
begin
  files := dir.GetFiles(path);
  Memo1.Clear;
  for str in files do Memo1.Lines.Add(str);
end;

//获取指定目录下的所有指定类型的文件
procedure TForm1.Button2Click(Sender: TObject);
var
  files: TStringDynArray;
  str: string;
begin
  files := TDirectory.GetFiles(path, '*.txt');
  Memo1.Clear;
  for str in files do Memo1.Lines.Add(str);
end;

//获取指定目录及其嵌套目录下的所有指定类型的文件
procedure TForm1.Button3Click(Sender: TObject);
var
  files: TStringDynArray;
  str: string;
begin
  files := TDirectory.GetFiles(path, '*.txt', TSearchOption.soAllDirectories);
  Memo1.Clear;
  for str in files do Memo1.Lines.Add(str);
end;

end.

相关阅读 >>

Delphi下的纯pascal的十六进制转十进制

Delphi中判断一个字符的位置在字符串开头或者末尾

Delphi 之 列表框组件(tlistbox)

Delphi中的memo顶部添加行

Delphi中利用中断获得系统启动以来的时间

Delphi调用android振动功能

Delphi 老外分享的textfile高速遍历大数据文本

Delphi 字符串的分割

Delphi列表控件tlistview定位到某一行

Delphi读取网页源文件和获取字符串

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



打赏

取消

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

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

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

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

评论

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