WIP: file filter pattern

This commit is contained in:
Ray 2021-10-27 02:04:11 +01:00
parent cb9f5eb663
commit 0c01ab92c1
3 changed files with 115 additions and 4 deletions

View File

@ -11,7 +11,6 @@ namespace RandomFileRunner.IO
public class SmartFileSystem public class SmartFileSystem
{ {
public static List<string> GetFiles(string path, string pattern) public static List<string> GetFiles(string path, string pattern)
{ {
List<string> fileList = new List<string>(); List<string> fileList = new List<string>();
@ -72,6 +71,8 @@ namespace RandomFileRunner.IO
List<string> directoryList = new List<string>(); List<string> directoryList = new List<string>();
ulong searchCount = 0; ulong searchCount = 0;
if (string.IsNullOrWhiteSpace(pattern)) pattern = "*";
directoryList.Add(path); directoryList.Add(path);
searchCount++; searchCount++;
@ -113,8 +114,13 @@ namespace RandomFileRunner.IO
callback(null, searchCount, directoryList.Count); 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)) if (!IsFileAccessible(item))
{ {
continue; continue;
@ -162,5 +168,110 @@ namespace RandomFileRunner.IO
return true; return true;
} }
public static bool MatchFileSearchPattern(string pattern, string subject)
{
if (pattern.Contains(';'))
{
string[] parts = pattern.Split(';');
for (int i=0; i<parts.Length; i++)
{
if (string.IsNullOrWhiteSpace(parts[i]))
{
continue;
}
if (MatchWildcard(parts[i].Trim(), subject))
{
return true;
}
}
return false;
}
else
{
return MatchWildcard(pattern, subject);
}
}
public static bool MatchWildcard(string wildcardPattern, string subject)
{
if (string.IsNullOrWhiteSpace(wildcardPattern))
{
return false;
}
Func<string, string, bool> 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);
}
}
} }
} }

2
MainForm.Designer.cs generated
View File

@ -451,7 +451,7 @@ namespace RandomFileRunner
this.Controls.Add(this.pictureBox1); this.Controls.Add(this.pictureBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "MainForm"; this.Name = "MainForm";
this.Text = "Random File"; this.Text = "222";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.menuStrip1.ResumeLayout(false); this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout(); this.menuStrip1.PerformLayout();

View File

@ -135,7 +135,7 @@ namespace RandomFileRunner
if (Directory.Exists(item)) if (Directory.Exists(item))
{ {
SmartFileSystem.GetFiles(item, "*.txt", SearchDirecory_OnFound); SmartFileSystem.GetFiles(item, textBox1.Text, SearchDirecory_OnFound);
ThreadControl.SetText(label2, foundFiles.Count.ToString()); ThreadControl.SetText(label2, foundFiles.Count.ToString());