Colonel Panic
Colonel Panic

Reputation: 137524

How to make program accept glob (wildcards) at command line?

I would like users to be able to run my program (from Windows cmd) with syntax like this

The app would then do its business for each of the relevant files. In Unix this is called globbing and it's done by the shell.

How can I achieve this for a Windows C# command line app?

Hypothetical syntax

void Main(string[] args)
{
    foreach(var file in args.SelectMany(arg => Glob.Expand(arg)))
    {
        Process(file)
    }
}

Upvotes: 0

Views: 940

Answers (2)

Dick Baker
Dick Baker

Reputation: 71

take a look at NuGet package Microsoft.Extensions.FileSystemGlobbing

https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.filesystemglobbing.matcher

Upvotes: 1

Pieter van Ginkel
Pieter van Ginkel

Reputation: 29632

The easiest way to do this is to convert the command line parameter into a regular expression. See glob pattern matching in .NET for an example on how to convert the command line argument into a regular expression.

Upvotes: 0

Related Questions