From 56db8a2b97366dcf642408536084ca19521f6f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Notin?= Date: Wed, 22 Aug 2018 14:48:56 +0200 Subject: [PATCH] Improve FindFiles() performance by removing unecessary operations --- Seatbelt/Program.cs | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/Seatbelt/Program.cs b/Seatbelt/Program.cs index 9bd28ec..793659b 100755 --- a/Seatbelt/Program.cs +++ b/Seatbelt/Program.cs @@ -1648,37 +1648,28 @@ namespace Seatbelt public static List FindFiles(string path, string patterns) { + // finds files matching one or more patterns under a given path, recursive // adapted from http://csharphelper.com/blog/2015/06/find-files-that-match-multiple-patterns-in-c/ // pattern: "*pass*;*.png;" - string[] pattern_array = patterns.Split(';'); - var files = new List(); - foreach (string pattern in pattern_array) + + try { - try + // search every pattern in this directory's files + foreach (string pattern in patterns.Split(';')) { files.AddRange(Directory.GetFiles(path, pattern, SearchOption.TopDirectoryOnly)); - foreach (var directory in Directory.GetDirectories(path)) - files.AddRange(FindFiles(directory, pattern)); } - catch (UnauthorizedAccessException) { } - catch (PathTooLongException) { } + + // go recurse in all sub-directories + foreach (var directory in Directory.GetDirectories(path)) + files.AddRange(FindFiles(directory, patterns)); } + catch (UnauthorizedAccessException) { } + catch (PathTooLongException) { } return files; - - //foreach (string pattern in pattern_array) - //{ - // foreach (string filename in Directory.GetFiles(dir_name, pattern, SearchOption.AllDirectories)) - // { - // if (!files.Contains(filename)) files.Add(filename); - // } - //} - - //files.Sort(); - - //return files; } public static IEnumerable Split(string text, int partLength)