Is there a wildcard expansion option for .net apps?

Here us my rough hack. I'd love for it to be recursive. And having experienced the shortcoming of Windows wildcards I might decide to use regular expressions rather than letting GetFiles() do it for me.

using System.IO;

public static string[] ExpandFilePaths(string[] args)
{
    var fileList = new List<string>();

    foreach (var arg in args)
    {
        var substitutedArg = System.Environment.ExpandEnvironmentVariables(arg);

        var dirPart = Path.GetDirectoryName(substitutedArg);
        if (dirPart.Length == 0)
            dirPart = ".";

        var filePart = Path.GetFileName(substitutedArg);

        foreach (var filepath in Directory.GetFiles(dirPart, filePart))
            fileList.Add(filepath);
    }

    return fileList.ToArray();
}

I'm not sure exactly what you're after... but if I get where you're going with the Directory.GetFiles() "hack" you mentioned, then something like this might work:

var Dirs = Directory.GetDirectories(@"C:\Windows", "sys*", 
SearchOption.TopDirectoryOnly).ToList();
var Files = new List<String>();
Dirs.ForEach(dirName => Files.AddRange(Directory.GetFiles(dirName, "*.sys", SearchOption.AllDirectories)));

The wildcard option on the GetDirectories call will allow you to grab all the directories contained in the Windows folder [directly] that match the pattern "sys*".

You can then iterate over those folders grabbing all the files that match the pattern "*.sys".

Is that the kind of thing you're looking for? To automatically expand the args, you'd have to extract the wildcards in some kind of meaningful manner and apply them to that model...

For instance:

RunMyApp "C:\Windows\Sys*\ *.sys"

You'd pull out the string C:\Windows - probably with a regular expression, find the lowest level directory that doesn't contain a wildcard and apply it to the GetDirectories method, attaching the wildcarded string as the search parameter.

Then if your end of string (in this case *.sys) as the search pattern for Directory.GetFiles.

If you wanted to get more complicated and do something like:

C:\Windows\*\Sys*\*.sys

You would use the SearchOptions to set this behaviour:

Directory.GetDirectories(@"C:\Windows", "sys*", SearchOptions.AllDirectories)

This would grab all directories that matched the sys* wildcard in the Windows directory and all directories below it.

If you wanted to get much more complicated than that, then I'm not sure how you would do that... for instance, say you wanted folders that are contained by folders directly inside the Windows directory - I have no idea how you would go about something like that I'm afraid...I don't imagine exporting the entire tree structure to XML and using XPath to do it would be so efficient - the XPath would be fabulously simple for parsing out using wildcards - but converting to XML wouldn't be so efficient...


see the source code of disassembled Microsoft.Build.Shared.FileMatcher class in Microsoft.Build.dll you can get some idea from the implementation of method GetFiles.

as a client you may use the method as follows

var filelist = FileMatcher.GetFiles(@"c:\alaki",@"c:\alaki\**\bin\*.dll");

Your code looks like exactly how you're supposed to do it.


Comments

  1. Zaid

    • 2021/5/20

    net applications. Is there a standard way to have your app's command line parameters automatically wildcard expanded? (i.e. expand *.doc from 

  2. Frank

    • 2019/2/25

    I've used the setargv.obj linking for Expanding Wildcard Arguments in the past for a number of C and C++ apps, but I can't find any similar mention for .net applications. Is there a standard way t

  3. Nash

    • 2016/11/24

    I've used the setargv.obj linking for Expanding Wildcard Arguments in the past for a number of C and C++ apps, but I can't find any similar mention for .net 

  4. Jamie

    • 2021/2/28

    3 Is there a wildcard expansion option for .net apps? Jul 16 '12. 2 Is there a way to find what Types are referenced by a c# assembly? Jun 25 '12.

  5. Esteban

    • 2020/12/16

    Is there a wildcard expansion option for .net apps? I've used the setargv.obj linking for Expanding Wildcard Arguments in the past for a number of C and C++ 

  6. Heath

    • 2020/10/15

    Wildcard expansion is * Work around dotnet/cli-migrate#11 * Upgrade to .NET Core 1.1.x The problem is that the wildcard expansion is skipped if there is any

  7. Malachi

    • 2020/4/2

    I've used the setargv.obj linking for Expanding Wildcard Arguments in the past for a number of C and C++ apps, but I can't find any similar mention for .net 

  8. Jake

    • 2019/7/16

    Is there a wildcard expansion option for .net apps? get back hidden FORM from another FORM; Elegant way to validate values; Can't download files from the computer with enabled TLS 1.1/1.2 protocols using WebClient.DownloadFile method; Using C# 7.2 in modifier for parameters with primitive types; RichTextBox Newline Conversion?

  9. Alvin

    • 2016/10/17

    NET class that implements the cmdlet. The following code shows how to set wildcard options and define the wildcard pattern used for 

  10. Muhammad

    • 2019/4/2

    When you design cmdlets that support wildcard characters, allow for combinations of wildcard characters. For example, the following command uses the Get-ChildItem cmdlet to retrieve all the .txt files that are in the c:\Techdocs folder and that begin with the letters "a" through "l." Get-ChildItem c:\techdocs\ [a-l]\*.txt.

  11. Edward

    • 2019/9/20

    A Hands-On Guide to Building iOS Applications with C# and .NET project selected in the solution tree, select Project > LMT1 Options from the main menu.

  12. Mylo

    • 2017/11/29

    The "app" in question has a subdirectory for every installed version, which ends in a two-digit version number. The above line is supposed to set %APPDIR% to the highest installed version. In TC, the wildcards are not expanded.

  13. Judson

    • 2016/4/17

    NET applications and dynamically loaded libraries. NOTE: This option is only supported on Windows 10, version 1803, and above. No. 20 Enabled: 

  14. Arthur

    • 2017/12/17

    Some of the options mention ILLink, which is the name of the underlying tool that implements trimming. For more information about the underlying tool, see the Trimmer documentation. Trimming with PublishTrimmed was introduced in .NET Core 3.0. The other options are available only in .NET 5 and above. Enable trimming <PublishTrimmed>true

  15. Westley

    • 2019/9/12

    What I would like to do is swop this out for a pattern, because the first part of the filename Is there a wildcard expansion option for .net apps?

  16. Mathew

    • 2019/12/25

    Note: Some command shells expand wildcard matches prior to running the gsutil The -p option can be used to specify a project other than the default.

  17. Jayden

    • 2019/10/3

    Unfortunately, while COPY does support wildcard expansion, the above snippet doesn't do what you'd like it to. Instead of copying each of 

Comments are closed.

Recent Posts