How to Implement Glob in C#

glob pattern matching in .NET

I found the actual code for you:

Regex.Escape( wildcardExpression ).Replace( @"\*", ".*" ).Replace( @"\?", "." );

How do I get a list of files in c# from a directory path which contains wildcards?

See: How to implement glob in C#

How to use FileSystemGlobbing.Matcher

You have to specify what to include:

var matcher = new Matcher();
matcher.AddInclude("**"); // <-- this line is added
matcher.AddExclude("foo/*.txt");
matcher.Match(new[] { "foo/a.txt", "foo/b.md", "bar/a.txt" });

Using relative paths and globbing patterns (all files in folder/subfolders) in bundleconfig.json with BundlerMinifier

Note: the following is a hacky workaround that I implemented -- it is not an official solution to this problem, though it may still be helpful.

I cloned the BundlerMinifier project so I could debug how it was bundling the files specified in bundleconfig.json and see what the problem was. The issue was in Bundle.cs, in particular in the GetAbsoluteInputFiles method. This line gets the path of the folder in which bundleconfig.json is stored:

string folder = new DirectoryInfo(Path.GetDirectoryName(FileName)).FullName;

The issue is that this same folder variable is used later when trimming the start of the paths of the files that are found:

var allFiles = Directory.EnumerateFiles(searchDir, "*" + ext, SearchOption.AllDirectories).Select(f => f.Replace(folder + FileHelpers.PathSeparatorChar, ""));

Since the files were in another directory, the .Select(f => f.Replace()); part didn't remove the start of the paths of the files, which meant the comparisons later failed when being matched. Thus, no files with both ../ and a globbing pattern were found.

I came up with a hacky solution for myself, but I don't think it's robust and I will therefore not contribute to the Git project. Nonetheless, I'll put it here in case anyone else has the same issue. I created a copy of folder:

string folder = new DirectoryInfo(Path.GetDirectoryName(FileName)).FullName;
string alternateFolder = folder;
bool folderModified = false;

Then I created a copy of inputFile and checked if it starts with ../. If so, remove it and at the same time remove the last folder name from alternateFolder:

string searchDir = new FileInfo(Path.Combine(folder, relative).NormalizePath()).FullName;
string newInput = inputFile;
while (newInput.StartsWith("../"))
{
// I'm sure there's a better way to do this using some class/library.
newInput = newInput.Substring(3);
if (!folderModified)
{
int lastSlash = alternateFolder.LastIndexOf('\\');
alternateFolder = alternateFolder.Substring(0, lastSlash);
folderModified = true;
}
}

Then I use alternateFolder and newInput only in the following lines:

var allFiles = Directory.EnumerateFiles(searchDir, "*" + ext, SearchOption.AllDirectories).Select(f => f.Replace(alternateFolder + FileHelpers.PathSeparatorChar, ""));
var matches = Minimatcher.Filter(allFiles, newInput, options).Select(f => Path.Combine(alternateFolder, f));

Everywhere else still uses folder and inputFile. Also note the use of the folderModified boolean. It is important to only remove the last folder on alternateFolder once since it is in a foreach loop.

Matching strings with wildcard

You could use the VB.NET Like-Operator:

string text = "x is not the same as X and yz not the same as YZ";
bool contains = LikeOperator.LikeString(text,"*X*YZ*", Microsoft.VisualBasic.CompareMethod.Binary);

Use CompareMethod.Text if you want to ignore the case.

You need to add using Microsoft.VisualBasic.CompilerServices; and add a reference to the Microsoft.VisualBasic.dll.

Since it's part of the .NET framework and will always be, it's not a problem to use this class.

Glob matching in C# - StringType.StrLike is apparently deprecated, whats the replacement?

Try using the LikeOperator instead.

using Microsoft.VisualBasic.CompilerServices;

...

if (LikeOperator.LikeString(left, right, CompareMethod.Text)) {
...
}


Related Topics



Leave a reply



Submit