FindFirst,FindNext,FindClose学习

procedure TForm1.Button1Click(Sender: TObject);
var
Dir: TSearchRec; //文件结构
begin
if FindFirst(edit1.Text,faAnyFile,Dir) = 0 then
begin
repeat
//是目录吗?
//and 的作用是通过 "与" fadirectory 消掉Attr中的其它文件,只取留下faDirectory属性
//通过 or 可以添另其它属性
if(Dir.Attr and faDirectory) = faDirectory then
begin
ListBox1.Items.Add(dir.Name);
if (dir.Name <> '.') and (dir.name <> '..') then
GetSubDir('','D:\'+dir.Name,5,true);
end;
until (FindNext(Dir) <> 0);
end;
FindCLose(Dir);
end;

procedure TForm1.GetSubDir(Filter, Folder: string;TreeLevel:Integer; Recurse: boolean);
var
Dir: TSearchRec;
count :Integer;
begin
count := 0;
Folder :=IncludeTrailingPathDelimiter(Folder);

if FindFirst(Folder+'*.*',faDirectory,Dir) = 0 then
begin
repeat
//是目录吗?
//and 的作用是通过 "与" fadirectory 消掉Attr中的其它文件,只取留下faDirectory属性
//通过 or 可以添另其它属性
if(Dir.Attr and faDirectory) = faDirectory then
begin
if (dir.Name <> '.') and (dir.name <> '..') then
begin
inc(count); //文件深度count

if(count > TreeLevel) then
break;

ListBox1.Items.Add(dir.Name);

if Recurse then
GetSubDir('',Folder+dir.Name,2,Recurse); //递归调用获取子目录
end;
end;
until (FindNext(Dir) <> 0);
end;
FindCLose(Dir);
end;

 

转载于:https://www.cnblogs.com/pengshaomin/archive/2012/02/08/2343047.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值