Search code examples
c#windows.net-corefile-search

.NET, Syntax for '<' and '>' wildcards in search pattern when EnumerationOptions MatchType = MatchType.Win32


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://learn.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.


Solution

  • 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.