获取目录下所有文件名的代码

编程学习 2021-07-05 12:58www.dzhlxh.cn编程入门
获取目录下所有文件名的代码,需要的朋友可以参考下。
一:获取指定文件夹的文件
代码如下:

procedure searchfile(path:string);//注意,path后面要有'\';
var
SearchRec:TSearchRec;
found:integer;
begin
found:=FindFirst(path+'*.*',faAnyFile,SearchRec);
while found=0 do
begin
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..')
and (SearchRec.Attr<>faDirectory) then
ListBox1.Add(SearchRec.Name);
found:=FindNext(SearchRec);
end;
FindClose(SearchRec);
end;

二:获取该目录下的子目录的文件
代码如下:

procedure SearchFile(path:string);
var
SearchRec:TSearchRec;
found:integer;
begin
found:=FindFirst(path+'*.*',faAanyFile,SearchRec);
while found=0 do
begin
if (SearchRec.Name<>'.') and (SearchRec.name<>'..) and
(SearchRec.Name=faDirectory) then
SearchFile(SearchRec.Name+'\')
else
LisBox1.Add(SearchRec.Name);
found:=FindNext(SearchREc);
end;
FindClose(SearchRec);
end;

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板