子目录级查找指定文件,添加到列表中原出自:无泪
procedure FindFiles(APath, AFile: string;FileList:Tstrings);
var
FindResult: integer;
FSearchRec, DSearchRec: TSearchRec;
function IsDirNotation(ADirName: string): Boolean;
begin
Result := ((ADirName = '.') or (ADirName = '..'));
end;
begin
if APath[Length(APath)] <> '/' then
APath := APath + '/';
FindResult := FindFirst(APath + AFile, faAnyFile +
faHidden +faSysFile + faReadOnly,
FSearchRec); //在根目录中查找指定文件
try
while FindResult = 0 do
begin
FileList.Add(APath + FSearchRec.Name);
FindResult := FindNext(FSearchRec); // 查找下一个指定文件
end;
//进入当前目录的子目录继续查找
FindResult := FindFirst(APath + '*.*', faDirectory, DSearchRec);
while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and
not IsDirNotation(DSearchRec.Name) then
//递归调用FindFiles函数
FindFiles(APath + DSearchRec.Name, AFile,FileList);
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
end;
end;
调用:
FindFiles('c:/','*.exe',listbox1.items);