Gehe zu deutscher Webseite

ViaThinkSoft CodeLib

This article is in:
CodeLibProgramming aidsDelphi

// Recursive procedure to build a list of files
procedure FindFiles(FilesList: TStrings; StartDir, FileMask: string);
var
  SR: TSearchRec;
  DirList: TStrings;
  IsFound: Boolean;
  i: integer;
begin
  if StartDir[length(StartDir)] <> PathDelim then
    StartDir := StartDir + PathDelim;

  IsFound :=
    FindFirst(StartDir+FileMask, faAnyFile-faDirectory, SR) = 0;
  while IsFound do begin
    FilesList.Add(StartDir + SR.Name);
    IsFound := FindNext(SR) = 0;
  end;
  FindClose(SR);

  // Build a list of subdirectories
  DirList := TStringList.Create;
  IsFound := FindFirst(StartDir+'*.*', faAnyFile, SR) = 0;
  while IsFound do begin
    if ((SR.Attr and faDirectory) <> 0) and
         (SR.Name[1] <> '.') then
      DirList.Add(StartDir + SR.Name);
    IsFound := FindNext(SR) = 0;
  end;
  FindClose(SR);

  // Scan the list of subdirectories
  for i := 0 to DirList.Count - 1 do
    FindFiles(FilesList, DirList[i], FileMask);

  DirList.Free;
end;

Quelle: http://www.festra.com/eng/snip04.htm (modifiziert)

Alternativer Code

procedure ListFiles(Directory: string; list: TStrings; recursive: boolean);
var
  SR: TSearchRec;
begin
  Directory := IncludeTrailingPathDelimiter(Directory);

  if Application.Terminated then exit;
  Application.ProcessMessages;

  if FindFirst(Directory+'*', faAnyFile, SR) = 0 then;
  begin
    repeat
      if (SR.Name <> '.') and (SR.Name <> '..') then
      begin
        if SR.Attr and faDirectory = faDirectory then
        begin
          if recursive then
          begin
            ListFiles(Directory + SR.Name, list, recursive);
          end;
        end
        else
        begin
          list.Add(Directory + SR.Name);
        end;
      end;
    until FindNext(SR) <> 0;
  end;
  FindClose(SR);
end;

Quelle: Extended Duplicate 2.0
Daniel Marschall
ViaThinkSoft Co-Founder