super Idee, und schon eingebaut
Delphi-Quellcode:
procedure FindAllFiles(FileList: TStrings; RootFolder: string; Mask: string = '*'; Recurse: Boolean = True; AddFolderNames: Boolean = False; IgnoreMaskAtFolderNames: Boolean = True);
procedure LFindAllFiles(AParentFolder: string; AMasks: TStrings);
function LIsInMasks(AFilename: String): Boolean;
var LCount: Integer;
begin
result := False;
for LCount := 0 to AMasks.Count - 1 do
begin
if Like(AMasks.Strings[LCount], AFilename) then
begin
result := True;
break;
end;
end;
end;
var LSearchRec : TSearchRec;
begin
if FindFirst(AParentFolder + '*', faAnyFile, LSearchRec) = 0 then
begin
repeat
if (LSearchRec.Name <> '.') and (LSearchRec.Name <> '..') then
begin
if LSearchRec.Attr and faDirectory = faDirectory then
begin
if AddFolderNames and
(IgnoreMaskAtFolderNames or LIsInMasks(AnsiLowerCase(LSearchRec.Name))) then
FileList.AddObject(AParentFolder + LSearchRec.Name, TObject(True));
if Recurse then
LFindAllFiles(AParentFolder + LSearchRec.Name + '\', AMasks);
end
else if LIsInMasks(AnsiLowerCase(LSearchRec.Name)) then
FileList.AddObject(AParentFolder + LSearchRec.Name, TObject(False));
end;
until FindNext(LSearchRec) <> 0;
FindClose(LSearchRec);
end;
end;
var LMasks: TStringList;
begin
LMasks := TStringList.Create;
LMasks.Sorted := True;
LMasks.Duplicates := dupIgnore;
LMasks.Delimiter := '|';
LMasks.DelimitedText := AnsiLowerCase(Mask);
LFindAllFiles(IncludeTrailingPathDelimiter(RootFolder), LMasks);
LMasks.Free;
end;
Als Delimiter wird "|" verwendet da dieses Zeischen nicht im Dateinamen vorkommen kann.
Beispielaufruf (Zeigt alle *.txt und *.sys auf Laufwerk C und Unterordnern an)
Delphi-Quellcode:
ListBox1.Items.BeginUpdate;
FindAllFiles(ListBox1.Items, 'c:', '*.txt|*.sys', True);
ListBox1.Items.EndUpdate;