From 0c01ab92c1e0222a5e84709bf3dc7de8862550d4 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 27 Oct 2021 02:04:11 +0100 Subject: [PATCH] WIP: file filter pattern --- IO/SmartFileSystem.cs | 115 +++++++++++++++++++++++++++++++++++++++++- MainForm.Designer.cs | 2 +- MainForm.cs | 2 +- 3 files changed, 115 insertions(+), 4 deletions(-) diff --git a/IO/SmartFileSystem.cs b/IO/SmartFileSystem.cs index bb37373..bed749f 100644 --- a/IO/SmartFileSystem.cs +++ b/IO/SmartFileSystem.cs @@ -11,7 +11,6 @@ namespace RandomFileRunner.IO public class SmartFileSystem { - public static List GetFiles(string path, string pattern) { List fileList = new List(); @@ -72,6 +71,8 @@ namespace RandomFileRunner.IO List directoryList = new List(); ulong searchCount = 0; + if (string.IsNullOrWhiteSpace(pattern)) pattern = "*"; + directoryList.Add(path); searchCount++; @@ -113,8 +114,13 @@ namespace RandomFileRunner.IO callback(null, searchCount, directoryList.Count); } - foreach (string item in Directory.EnumerateFiles(directory, pattern, SearchOption.TopDirectoryOnly)) + foreach (string item in Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly)) { + if (!MatchFileSearchPattern(pattern, Path.GetFileName(item))) + { + continue; + } + if (!IsFileAccessible(item)) { continue; @@ -162,5 +168,110 @@ namespace RandomFileRunner.IO return true; } + public static bool MatchFileSearchPattern(string pattern, string subject) + { + if (pattern.Contains(';')) + { + string[] parts = pattern.Split(';'); + for (int i=0; i matchPattern = (pattern, subject) => + { + string[] parts = pattern.Split('*'); + if (parts.Length <= 1) + { + return subject.Equals(pattern, StringComparison.CurrentCultureIgnoreCase); + } + + int pos = 0; + + for (int i = 0; i < parts.Length; i++) + { + if (i <= 0) + { + // first + pos = subject.IndexOf(parts[i], pos, StringComparison.CurrentCultureIgnoreCase); + if (pos != 0) + { + return false; + } + } + else if (i >= (parts.Length - 1)) + { + // last + if (!subject.EndsWith(parts[i], StringComparison.CurrentCultureIgnoreCase)) + { + return false; + } + } + else + { + pos = subject.IndexOf(parts[i], pos, StringComparison.CurrentCultureIgnoreCase); + if (pos < 0) + { + return false; + } + + pos += parts[i].Length; + } + } + + return true; + }; + + int wildcardCount = wildcardPattern.Count(x => x.Equals('*')); + if (wildcardCount <= 0) + { + return subject.Equals(wildcardPattern, StringComparison.CurrentCultureIgnoreCase); + } + else if (wildcardCount == 1) + { + string newWildcardPattern = wildcardPattern.Replace("*", ""); + + if (wildcardPattern.StartsWith("*")) + { + return subject.EndsWith(newWildcardPattern, StringComparison.CurrentCultureIgnoreCase); + } + else if (wildcardPattern.EndsWith("*")) + { + return subject.StartsWith(newWildcardPattern, StringComparison.CurrentCultureIgnoreCase); + } + else + { + return matchPattern(wildcardPattern, subject); + } + } + else + { + return matchPattern(wildcardPattern, subject); + } + } + } } diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 4864909..ac4d541 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -451,7 +451,7 @@ namespace RandomFileRunner this.Controls.Add(this.pictureBox1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "MainForm"; - this.Text = "Random File"; + this.Text = "222"; ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); diff --git a/MainForm.cs b/MainForm.cs index 7db8dee..094d3e9 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -135,7 +135,7 @@ namespace RandomFileRunner if (Directory.Exists(item)) { - SmartFileSystem.GetFiles(item, "*.txt", SearchDirecory_OnFound); + SmartFileSystem.GetFiles(item, textBox1.Text, SearchDirecory_OnFound); ThreadControl.SetText(label2, foundFiles.Count.ToString());