Searching for directories with wildcard mid in path

82 views Asked by At

I want to get all directories that match a search-pattern with a wildcard mid in the path, e.g.

C:\\folder1\\folder2\\*\\nextFolder\\lastFolder

Is there any chance to get them with a function-call?

I've tried to use DirectoryInfo.GetDirectories(), but it seems, that for this function a wildcard is only allowed inside the last part of the path, as in:

C:\\folder1\\folder2\\folder3\\nextFolder\\last*

or

C:\\folder1\\folder2\\folder3\\nextFolder\\*

Here some additional information and the sourcecode I used with the given feedback: Environment: Windows, Visual Studio 2022, .NET Framework 4.6.2 Existing directories: C:\MESDataTransfer\Csv_Cnc_to_Mes_Prod\job\ABD\MultiParts01 C:\MESDataTransfer\Csv_Cnc_to_Mes_Prod\job\BHX500\MultiParts01 As result I get directoryPaths.Message = "Die Anfrage ist ungültig" and directoryPaths.ErrorCode = InvalidQuery. Same error, if I replace "%" with a existing foldername "ABD". Is the syntax of @"\.\ROOT\cimv2" for ManagementScope correct? Seems to be the value for UTF16. Is this always correct?

var scope = new ManagementScope(@"\\.\ROOT\cimv2");
string[] selectProps = { "Path" };
var query = new SelectQuery("Win32_Directory",
    @"Drive = 'C:' and Path Like 'MESDataTransfer\Csv_Cnc_to_Mes_Prod\job\%\MultiParts01'",
    selectProps);

using (var searcher = new ManagementObjectSearcher(scope, query))
{
    using (var instances = searcher.Get())
    {
        var directoryPaths = instances.Cast<ManagementObject>().Select(mo => mo["Path"]);

        // enumerate the collection.
        int nCount = 0;
        foreach (string directoryPath in directoryPaths)
        {
            //Console.WriteLine(directoryPath);
            string asString = directoryPath.ToString();
            ++nCount;
        }
    }
}
1

There are 1 answers

2
lidqy On

You could do it with a WMI directory query.
It accepts wildcards for any part of the path, not just for the token at the end, aka the name.
The pendant for the Win32 '*' wildcard is a '%'.

    // needs a reference to System.Management (nuget or assembly)
    
    var scope = new ManagementScope(@"\\.\ROOT\cimv2");

    string[] selectProps = { "Path" };
    var query = new SelectQuery("Win32_Directory",
          @"Drive = 'C:' and Path Like 'folder1\folder2\%\nextFolder\lastFolder'", 
          selectProps);

    using var searcher  = new ManagementObjectSearcher(scope, query);
    using var instances = searcher.Get();
    var directoryPaths  = instances.Cast<ManagementObject>().Select(mo => mo["Path"]);

    // enumerate the collection.
    foreach (string directoryPath in directoryPaths)
    {
        Console.WriteLine(directoryPath);
    }

If you need more details of the directories returned, than just the "Path" itself, you can add the properties wanted to the 'selectProps' array. Win32_Directory has about the same set of properties, if not more, as System.IO.DirectoryInfo has: https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-directory