Home > Enterprise >  .NET, Syntax for '<' and '>' wildcards in search pattern when EnumerationO
.NET, Syntax for '<' and '>' wildcards in search pattern when EnumerationO

Time:02-03

The .NET documentation for MatchType.Win32 for Net-Core and Net 5 versions says:

Match using Win32 DOS style matching semantics.
'*', '?', '<', '>', and '"' are all considered wildcards. Matches in a traditional DOS / Windows command prompt way.

https://docs.microsoft.com/en-us/dotnet/api/system.io.matchtype?view=net-6.0

How do you use the syntax for the '<' and '>' wildcards? I can not find that anywhere and the only thing I can find about DOS is the '*' and '?' wildcards. I know in Windows Explorer you can do a search like "date:>01/01/2021" but that is more of an operator than a wildcard and gives a syntax exception if using it with

var query = Directory.EnumerateDirectories(
       basefolder,
       "date:>01/01/2021", 
       new EnumerationOptions() { MatchType = MatchType.Win32 });

Passing regular old "*" as searchPattern still returns all entries, trying to pass "<" or ">" alone as searchPattern did not produce any results on folders I tried.

CodePudding user response:

The logic for handling < and > is documented in inline comments here: https://github.com/dotnet/runtime/blob/c5c7967ddccc46c84c98c0e8c7e00c3009c65894/src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemName.cs

Specifically:

https://github.com/dotnet/runtime/blob/c5c7967ddccc46c84c98c0e8c7e00c3009c65894/src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemName.cs#L272

// '<' (DOS_STAR) matches any character except '.' zero or more times.`
// If we are at a period, determine if we are allowed to consume it, i.e. make sure it is not the last one.

https://github.com/dotnet/runtime/blob/c5c7967ddccc46c84c98c0e8c7e00c3009c65894/src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemName.cs#L309

// '>' (DOS_QM) is the most complicated. If the name is finished, // we can match zero characters. If this name is a '.', we don't match, but look at the next expression. Otherwise we match a single character.

CodePudding user response:

Only two wildcards can be used in your case here: * and ?

Maybe you could consider using a few Linq expressions to filter the returned collection ?

I think it is more precise if the timezone is important to you in your code.

  •  Tags:  
  • Related