Код:
procedure SearchFiles(ASearchPath : String;
AListBox1 : TListBox; AExtList1 : Array Of String;
AListBox2 : TListBox; AExtList2 : Array Of String);
function IsInList(AFileName : String; AExtList : Array Of String) : Boolean;
var
AExt : String;
I : Integer;
begin
Result := False;
AExt := ExtractFileExt(AFileName);
For I := Low(AExtList) To High(AExtList) Do
Begin
Result := AnsiCompareText(AExt,AExtList[i]) = 0;
If Result Then Break;
End;
end;
var
iFound : Integer;
F : TSearchRec;
begin
ASearchPath := IncludeTrailingPathDelimeter(ASearchPath) + '*.*';
iFound := FindFirst(ASearchPath,faAnyFile,F);
While iFound = 0 Do
Begin
If (F.Attr And faDirectory) = 0 Then // Not a folder
If (F.Name <> '.') And (F.Name <> '..') Then
Begin
If IsInList(F.Name,AExtList1) Then AListBox1.Items.Add(F.Name);
If IsInList(F.Name,AExtList2) Then AListBox2.Items.Add(F.Name);
End;
iFound := FindNext(F);
End;
FindClose(F);
end;
Принимает на вход:
-путь, где искать файлы (папка)
-первый ListBox;
-массив расширений файлов, которые надо добавить в первый ListBox (в виде '.ext');
-второй ListBox;
-массив расширений файлов, которые надо добавить во второй ListBox (в виде '.ext');
Пример испольхования:
Код:
procedure TForm1.Button1Click(Sender : TObject);
var
APath : Stirng;
begin
ListBox1.Items.Clear;
ListBox2.Items.Clear;
APath := ExtractFilePath(Application.ExeName); // Search in program's folder
SearchFiles(APath,ListBox1,['.exe'.'.dll'],ListBox2,['.txt','.dat']);
end;
В примере в первый список попадают модули программы (exe и dll), во второй - текстовые файлы и файлы с данными.