Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple file extensions to TDirectory.GetFiles?

TDirectory.GetFiles has a parameter called SearchPattern. Embarcadero's documentation says

The mask used when matching file names (for example, "*.exe" matches all the executable files).

However, I want to pass multiple file types. I get those types from a FilterComboBox.Mask. So, it is a string that looks like '*.txt;*.rtf;*.doc'.

I have tried to pass that string directly to GetFiles and it doesn't work. Do I have to parse it, break it into pieces and feed every individual piece to GetFiles?

like image 338
Server Overflow Avatar asked Oct 04 '12 11:10

Server Overflow


People also ask

How to filter multiple extension files from a given directory?

You can use the following expression to filter the multiple extension files from a given directory. files = Directory.GetFiles ("C:\path", "*.*", SearchOption.AllDirectories).Where (Function (s) s.EndsWith (".mp3") Or s.EndsWith (".jpg"))

How do I get multiple file extensions in c programmatically?

C# getfiles multiple extensions Since getfiles () can only get one type of file at a time, you can only get all the files in the specified directory first, recycle to determine whether each file is the file to be got; if you get all the video files in the specified directory, the implementation code is as follows: FileInfo [] afi = di.

How to get video files from a specific directory using getfiles ()?

Since getfiles () can only get one type of file at a time, you can only get all the files in the specified directory first, recycle to determine whether each file is the file to be got; if you get all the video files in the specified directory, the implementation code is as follows: FileInfo [] afi = di. GetFiles ( "*.*" );

How do I get a file with a specified extension?

C# directory getfiles get a file with a specified extension If you want to get all the .dat files in the specified directory, the same method is used, except that *.* is changed to *.dat. The code is as follows: FileInfo [] afi = di. GetFiles ( "*.dat" ); 3. C# getfiles multiple extensions


1 Answers

The RTL code behind GetFiles calls Masks.MatchesMask to test for a match to your search pattern. This function only supports masking against a single mask.

The alternative is to use the GetFiles overload that admits a TFilterPredicate. You supply a predicate that tests whether or not a name matches your pattern.

uses
  StrUtils, Types, Masks, IOUtils;

function MyGetFiles(const Path, Masks: string): TStringDynArray;
var
  MaskArray: TStringDynArray;
  Predicate: TDirectory.TFilterPredicate;
begin
  MaskArray := SplitString(Masks, ';');
  Predicate :=
    function(const Path: string; const SearchRec: TSearchRec): Boolean
    var
      Mask: string;
    begin
      for Mask in MaskArray do
        if MatchesMask(SearchRec.Name, Mask) then
          exit(True);
      exit(False);
    end;
  Result := TDirectory.GetFiles(Path, Predicate);
end;

Do note that MatchesMask creates and destroys a heap allocated TMask every time it is called. I can well imagine that being a performance bottleneck over a long search. In which case you could create an array of TMask objects from MaskArray. And use those in the predicate to test. I've no idea whether this is a valid concern or not, just something that occurred to me whilst perusing the code.

like image 63
David Heffernan Avatar answered Oct 07 '22 02:10

David Heffernan