Hallo!
Ich habe mir eine Routine geschrieben, die rekursiv die in einem Ordner enthaltenen Dateien in einem parallelen Task zählt:
Delphi-Quellcode:
uses
System.Threading;
procedure TForm1.CountFilesTask(const ADir: string);
var
ThisTask: ITask;
begin
ThisTask := TTask.Create(
procedure()
var
count: Integer;
procedure FileSearch(const ThisDir: string);
var
SR: TSearchRec;
begin
if FindFirst(IncludeTrailingPathDelimiter(ThisDir) + '*', faAnyFile, SR) = 0 then
begin
try
repeat
if (SR.Attr and faDirectory) = 0 then
begin
Inc(Count);
TThread.Synchronize(nil,
procedure
begin
lblShowFilesCount.Caption := IntToStr(count) + ' files';
end);
end
else if (SR.Name <> '.') and (SR.Name <> '..') then
begin
FileSearch(IncludeTrailingPathDelimiter(ThisDir) + SR.Name);
end;
until FindNext(SR) <> 0;
finally
FindClose(SR);
end;
end;
end;
begin
CodeSite.Send('VOR Suche');
FileSearch(ADir);
CodeSite.Send('NACH Suche');
end);
ThisTask.Start;
end;
procedure TForm1.btnStartCountClick(Sender: TObject);
begin
CountFilesTask(Trim(edtDirectory.Text));
end;
Was haltet ihr davon? Könnte man das noch weiter optimieren?