delphi 获取根目录下的文件名及子目录下的文件名
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi 获取根目录下的文件名及子目录下的文件名相关的知识,希望对你有一定的参考价值。
参考技术A findfirst,findnext,findclose....搜索个例子给你:
procedure
searchfileex(const
dir,
ext:
string;
files:
tstrings);
var
found:
tsearchrec;
i:
integer;
dirs:
tstrings;
finished:
integer;
stopsearch:
boolean;
begin
stopsearch
:=
false;
dirs
:=
tstringlist.create;
finished
:=
findfirst(dir
+
'*.*',
63,
found);
while
(finished
=
0)
and
not
(stopsearch)
do
begin
if
(found.name
<>
'.')
then
begin
if
(found.attr
and
fadirectory)
=
fadirectory
then
dirs.add(dir
+
found.name)
else
if
pos(uppercase(ext),
uppercase(found.name))
>
0
then
files.add(dir
+
found.name);
end;
finished
:=
findnext(found);
end;
findclose(found);
if
not
stopsearch
then
for
i
:=
0
to
dirs.count
-
1
do
searchfileex(dirs[i],
ext,
files);
dirs.free;
end;
3
procedure
findsubdir(dirname:
string;
filestring:
tstrings);
var
searchrec:
tsearchrec;
begin
//找出所有下级子目录。
if
(findfirst(dirname
+
'*.*',
fadirectory,
searchrec)
=
0)
then
begin
if
isvaliddir(searchrec)
then
filestring.add(dirname
+
searchrec.name);
while
(findnext(searchrec)
=
0)
do
begin
if
isvaliddir(searchrec)
then
filestring.add(dirname
+
searchrec.name);
end;
end;
findclose(searchrec);
end;
function
isvaliddir(searchrec:
tsearchrec):
boolean;
begin
if
(searchrec.attr
=
16)
and
(searchrec.name
<>
'.')
and
(searchrec.name
<>
'..')
then
result
:=
true
else
result
:=
false;
end;
DELPHI如何获取某目录下的所有文件名?
//=====================================================================
// 函数名称: FindPathFiles
// 功能描述: 找指定目录下的文件
// 参 数: APath : 路径名称
// APropty : 属性名称(*.* | *.txt)
// AFiles : 文件列表
// IsAddPath: 是否增加路径
// 作者:
// 时间:
// 返 回 值:
// 说 明:
//=====================================================================
procedure FindPathFiles(const APath: string; AFiles: TStrings;
const APropty: String = ‘*.*‘; IsAddPath: Boolean = False);
var
FS: TSearchRec;
FPath: String;
AddPath: string;
begin
FPath := IncludeTrailingPathDelimiter(APath);
AddPath := IfThen(IsAddPath, FPath, ‘‘);
if FindFirst(FPath + APropty, faAnyFile, FS) = 0 then
begin
repeat
if //(FS.Name <> ‘.‘) and (FS.Name <> ‘..‘) and
((FS.Attr and faDirectory) <> faDirectory) then
AFiles.Add(AddPath + FS.Name);
until FindNext(FS) <> 0;
SysUtils.FindClose(FS);
end;
end;
//=====================================================================
// 函数名称: FindAllFiles
// 功能描述: 找指定目录下的所有文件
// 参 数: APath : 路径名称
// APropty : 属性名称(*.* | *.txt)
// AFiles : 文件列表
// IsAddPath: 是否增加路径
// 作者:
// 时间:
// 返 回 值:
// 说 明:
//=====================================================================
procedure FindAllFiles(const APath: string; AFiles: TStrings;
const APropty: String = ‘*.*‘; IsAddPath: Boolean = False);
var
FS: TSearchRec;
FPath: String;
AddPath: string;
begin
FPath := IncludeTrailingPathDelimiter(APath);
AddPath := IfThen(IsAddPath, FPath, ‘‘);
if FindFirst(FPath + APropty, faAnyFile, FS) = 0 then
begin
repeat
if (FS.Name <> ‘.‘) and (FS.Name <> ‘..‘) then
if ((FS.Attr and faDirectory) = faDirectory) then
FindAllFiles(FPath + FS.Name, AFiles, APropty, IsAddPath)
else
AFiles.Add(AddPath + FS.Name);
until FindNext(FS) <> 0;
SysUtils.FindClose(FS);
end;
end;
以上是关于delphi 获取根目录下的文件名及子目录下的文件名的主要内容,如果未能解决你的问题,请参考以下文章