欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-02 15:58:24
一:获取指定文件夹的文件 复制代码 代码如下: procedure searchfile(path:string);//注意,path后面要有'\'; var search...
一:获取指定文件夹的文件
复制代码 代码如下:

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;