Thema: Delphi GetFiles SearchPattern

Einzelnen Beitrag anzeigen

striderx
Online

Registriert seit: 11. Feb 2007
Ort: Bergisch Gladbach
207 Beiträge
 
Delphi 10.4 Sydney
 
#9

AW: GetFiles SearchPattern

  Alt 18. Nov 2024, 19:50
wie wäre es hiermit (Beispiel sExtensions: '*.jpg;*.jpeg;*.tif'):

Delphi-Quellcode:
function GetFilesEx(sPath, sExtensions: String;
                    var sdaFiles: tStringDynArray;
                    IncludeSubdirectories: Boolean): Boolean;

var
  I: Word;
  sdaMasks: tStringDynArray;
  Predicate: tDirectory.TFilterPredicate;

begin
  Result := False;

  if (sPath = '') or
     (not tDirectory.Exists(sPath)) then begin
     ShowMsg('ungültiger Pfad', 'GetFilesEx', mb_OK, mb_IconInformation);
     Exit;
  end;
  sPath := IncludeTrailingPathDelimiter(sPath);

  if sExtensions = 'then begin
     ShowMsg('ungültige Dateierweiterung(en)', 'GetFilesEx', mb_OK,
             mb_IconInformation);
     Exit;
  end;

  sdaMasks := SplitString(sExtensions, ';');
  for I := 0 to High(sdaMasks) do begin
      if Pos('.', sdaMasks[I]) = 1 then sdaMasks[I] := '*' + sdaMasks[I]
      else if Pos('*.', sdaMasks[I]) = 0 then sdaMasks[I] := '*.' + sdaMasks[I];
  end;

  Predicate :=
       function(const Path: string; const SearchRec: TSearchRec): Boolean

       var
         Mask: String;

       begin
         for Mask in sdaMasks do
             if MatchesMask(SearchRec.Name, Mask) then Exit(True);
         Exit(False);
       end;

  try
    if IncludeSubDirectories then
       sdaFiles := tDirectory.GetFiles(sPath, TSearchOption.soAllDirectories,
                                       Predicate)
    else sdaFiles := tDirectory.GetFiles(sPath, Predicate);
    Result := True;
  except
    on E: Exception do begin
       ShowMsg(E.Message, 'GetFilesEx', mb_OK, mb_IconError);
    end;
  end;
end;

(ShowMsg ist meine eigene Message-Box)
  Mit Zitat antworten Zitat