delphi 获取根目录下的文件名及子目录下的文件名
答案:2 悬赏:50
解决时间 2021-02-12 23:32
- 提问者网友:冥界祭月
- 2021-02-12 02:55
delphi 获取根目录下的文件名及子目录下的文件名
最佳答案
- 二级知识专家网友:晚安听书人
- 2021-02-12 03:15
给一个通用过程,直接调用,运行看是不是你想要的效果。
procedure GetChildFileList(AStrings: TStrings; ASourFile,
FileName: string); // 查找子目录
// AStrings存放路径, ASourceFile要查找的目录,FileName搜索的文件类型 若指定类型,则'*.jpg'or '*.png'
var
sour_path, sour_file: string;
TmpList: TStringList;
FileRec, subFileRec: TSearchrec;
i: Integer;
begin
if copy(ASourFile, Length(ASourFile), 1) <> '\' then
sour_path := IncludeTrailingPathDelimiter(Trim(ASourFile)) // 在路径后面加上反斜杠
else
sour_path := trim(ASourFile);
sour_file := FileName;
if not DirectoryExists(sour_path) then
begin
AStrings.Clear;
exit;
end;
TmpList := TStringList.Create;
TmpList.Clear;
if FindFirst(sour_path + '*.*', faAnyfile, FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) then
begin
if ((FileRec.Name <> '.') and (FileRec.Name <> '..')) then
GetChildFileList(AStrings, sour_path + FileRec.Name + '\', sour_file);
end;
until FindNext(FileRec) <> 0;
FindClose(FileRec);
if FindFirst(sour_path + FileName, faAnyfile, subFileRec) = 0 then
repeat
if ((subFileRec.Attr and faDirectory) = 0) then
TmpList.Add(sour_path + subFileRec.Name);
until FindNext(subFileRec) <> 0;
FindClose(subFileRec);
for i := 0 to TmpList.Count - 1 do
AStrings.Add(TmpList.Strings[i]);
TmpList.Free;
end;
调用:
procedure TForm2.SpeedButton5Click(Sender: TObject);
begin
GetChildFileList(ListBox1.Items, 'D:\Wyp\', '*.jpg'); // 目录自己定
GetChildFileList(ListBox1.Items, 'D:\Wyp\', '*.png');
end;这里是将查找的目录存放在ListBox里的。
在加载List时,由于Item太多,所以有一定的延时,而不是卡死。
希望能帮到你。
procedure GetChildFileList(AStrings: TStrings; ASourFile,
FileName: string); // 查找子目录
// AStrings存放路径, ASourceFile要查找的目录,FileName搜索的文件类型 若指定类型,则'*.jpg'or '*.png'
var
sour_path, sour_file: string;
TmpList: TStringList;
FileRec, subFileRec: TSearchrec;
i: Integer;
begin
if copy(ASourFile, Length(ASourFile), 1) <> '\' then
sour_path := IncludeTrailingPathDelimiter(Trim(ASourFile)) // 在路径后面加上反斜杠
else
sour_path := trim(ASourFile);
sour_file := FileName;
if not DirectoryExists(sour_path) then
begin
AStrings.Clear;
exit;
end;
TmpList := TStringList.Create;
TmpList.Clear;
if FindFirst(sour_path + '*.*', faAnyfile, FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) then
begin
if ((FileRec.Name <> '.') and (FileRec.Name <> '..')) then
GetChildFileList(AStrings, sour_path + FileRec.Name + '\', sour_file);
end;
until FindNext(FileRec) <> 0;
FindClose(FileRec);
if FindFirst(sour_path + FileName, faAnyfile, subFileRec) = 0 then
repeat
if ((subFileRec.Attr and faDirectory) = 0) then
TmpList.Add(sour_path + subFileRec.Name);
until FindNext(subFileRec) <> 0;
FindClose(subFileRec);
for i := 0 to TmpList.Count - 1 do
AStrings.Add(TmpList.Strings[i]);
TmpList.Free;
end;
调用:
procedure TForm2.SpeedButton5Click(Sender: TObject);
begin
GetChildFileList(ListBox1.Items, 'D:\Wyp\', '*.jpg'); // 目录自己定
GetChildFileList(ListBox1.Items, 'D:\Wyp\', '*.png');
end;这里是将查找的目录存放在ListBox里的。
在加载List时,由于Item太多,所以有一定的延时,而不是卡死。
希望能帮到你。
全部回答
- 1楼网友:颜值超标
- 2021-02-12 04:44
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;
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯