6

I'm trying to use the TDirectory.GetFiles function, but when I add a TSearchOptions third parameter to force a recursive search the compiler raises an error saying that soAllDirectories has not been declared.

uses System.IOutils,
     System.Types;

procedure TfrmConversio.btnConversioClick(Sender: TObject);
var FilesPas: TStringDynArray;
begin
  FilesPas := TDirectory.GetFiles('C:\Project', '*.pas', soAllDirectories);
  ProgressBar1.Max := Length(FilesPas);
end;

What am I doing wrong ?. I can see that constant in System.IOUtils.

Thank you.

Marc Guillot
  • 6,090
  • 1
  • 15
  • 42

1 Answers1

8

You need to write

TDirectory.GetFiles('C:\Project', '*.pas', TSearchOption.soAllDirectories);

The reason is that the compiler directive {$SCOPEDENUMS ON} is found before the definition of the TSearchOption type. This means precisely that you need to qualify the enumeration's constants with the type name.

From the documentation:

The $SCOPEDENUMS directive enables or disables the use of scoped enumerations in Delphi code. More specifically, $SCOPEDENUMS affects only definitions of new enumerations, and only controls the addition of the enumeration's value symbols to the global scope.

In the {$SCOPEDENUMS ON} state, enumerations are scoped, and enum values are not added to the global scope. To specify a member of a scoped enum, you must include the type of the enum.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 1
    Bravo. I wonder how difficult it would have been for the compiler to mention the state of SCOPEDENUMS in the 'not declared' message? Not very, I expect... – MartynA Aug 19 '20 at 19:55
  • 3
    It would be even nicer if the compiler were smart enough to recognize that the 3rd parameter is a `TSearchOption` and automatically search for `TSearchOption.soAllDirectories` if it can't find `soAllDirectories` by itself. – Remy Lebeau Aug 19 '20 at 20:24