Notifications
Clear all

[Closed] Use pattern with specific string

this is almost everything you need and example that shows the difference:

fn CreateFileOpsAssembly =
(
	source  = ""
	source += "using System;\n"
	source += "using System.Collections.Generic;\n"
	source += "using System.Text;\n"
	source += "using System.Text.RegularExpressions;\n"
	source += "using System.IO;\n"
	source += "namespace FileOps\n"
	source += "{\n"
	source += "    public class Access\n"
	source += "    {\n"
	source += "        private static void getFilesRecursive(string path, string searchPattern, ref List<string> files)\n"
	source += "        {\n"
	source += "            try\n"
	source += "            {\n"
	source += "                foreach (string p in Directory.GetDirectories(path)) getFilesRecursive(p, searchPattern, ref files);\n"
	source += "                foreach (var file in Directory.GetFiles(path, searchPattern)) files.Add(file);\n"
	source += "            }\n"
	source += "            catch { }\n"
	source += "        }\n"
	source += "        public static List<string> GetFilesRecursive(string path, string searchPattern)\n"
	source += "        {\n"
	source += "            List<string> files = new List<string>();\n"
	source += "            getFilesRecursive(path, searchPattern, ref files);\n"
	source += "            return files;\n"
	source += "        }\n"
	source += "        private static string WildCardToRegular(string value)\n"
	source += "        {\n"
	source += "            return \"^\" + Regex.Escape(value).Replace(\"\\\\?\", \".\").Replace(\"\\\\*\", \".*\") + \"$\";\n"
	source += "        }\n"
	source += "        public static List<string> FindFilesInList(List<string> files, string searchWildcard)\n"
	source += "        {\n"
	source += "            List<string> match = new List<string>();\n"
	source += "            Regex regex = new Regex(WildCardToRegular(searchWildcard), RegexOptions.Compiled | RegexOptions.IgnoreCase);\n"
	source += "\n"
	source += "            foreach (var file in files) if (regex.IsMatch(file) == true) match.Add(file);\n"
	source += "            return match;\n"
	source += "        }\n"
	source += "    }\n"
	source += "}\n"

	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

	compilerParams.ReferencedAssemblies.AddRange #("System.dll")

	compilerParams.GenerateInMemory = on
	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
	
	assembly = compilerResults.CompiledAssembly
	assembly.CreateInstance "FileOps.Access"
)
global FileOps = CreateFileOpsAssembly()


(
	gc()
	h0 = heapfree

	t1 = timestamp()	
	bins = FileOps.GetFilesRecursive @"c:\windows" "*.bin"
	t2 = timestamp()
	dlls = FileOps.GetFilesRecursive @"c:\windows" "*.dll"
	t3 = timestamp()
	pngs = FileOps.GetFilesRecursive @"c:\windows" "*.png"
	t4 = timestamp()

	format "time:% (%, %, %) heap:%\n" (t4 - t1) (t2 - t1) (t3 - t2) (t4 - t3) (h0 - heapfree)
)

(
	gc()
	h0 = heapfree

	t1 = timestamp()	
	alls = FileOps.GetFilesRecursive @"c:\windows" "*"
	t2 = timestamp()	
	bins = FileOps.FindFilesInList alls "*.bin"
	t3 = timestamp()	
	dlls = FileOps.FindFilesInList alls "*.dll"
	t4 = timestamp()	
	pngs = FileOps.FindFilesInList alls "*.png"
	t5 = timestamp()	

	format "time:% (%, %, %, %) heap:%\n" (t5 - t1) (t2 - t1) (t3 - t2) (t4 - t3) (t5 - t4) (h0 - heapfree)
)

if you know exact path to the files everything can be much faster

we can split path and filename and search in filename only. there are many ways to optimize the algorithm. but the idea is the same.

thank you very much denis! that’s heavy but it’s something from which I can learn more

i thought a little more… all is easier and faster:

fn CreateFileOpsAssembly =
(
	source  = ""
	source += "using System;\n"
	source += "using System.Collections.Generic;\n"
	source += "using System.Text;\n"
	source += "using System.Text.RegularExpressions;\n"
	source += "using System.IO;\n"
	source += "namespace FileOps\n"
	source += "{\n"
	source += "    public class Access\n"
	source += "    {\n"
	source += "        private static void getFilesRecursive(string path, string searchPattern, ref List<string> files)\n"
	source += "        {\n"
	source += "            try\n"
	source += "            {\n"
	source += "                foreach (var sub in Directory.GetDirectories(path)) getFilesRecursive(sub, searchPattern, ref files);\n"
	source += "                foreach (var file in Directory.GetFiles(path, searchPattern)) files.Add(file);\n"
	source += "            }\n"
	source += "            catch { }\n"
	source += "        }\n"
	source += "        public static List<string> GetFilesRecursive(string path, string searchPattern)\n"
	source += "        {\n"
	source += "            List<string> files = new List<string>();\n"
	source += "            getFilesRecursive(path, searchPattern, ref files);\n"
	source += "            return files;\n"
	source += "        }\n"
	source += "        public static List<string> FindEndsWith(List<string> files, string filename)\n"
	source += "        {\n"
	source += "            filename = filename.ToLower();\n"
	source += "            List<string> match = new List<string>();\n"
	source += "            foreach (var file in files) if (file.ToLower().EndsWith(filename)) match.Add(file);\n"
	source += "            return match;\n"
	source += "        }\n"
	source += "    }\n"
	source += "}\n"

	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

	compilerParams.ReferencedAssemblies.AddRange #("System.dll")

	compilerParams.GenerateInMemory = on
	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
	
	assembly = compilerResults.CompiledAssembly
	assembly.CreateInstance "FileOps.Access"
)
global FileOps = CreateFileOpsAssembly()

(
	gc()
	h0 = heapfree

	t1 = timestamp()	
	alls = FileOps.GetFilesRecursive @"c:\windows" "*"
	t2 = timestamp()	
	bins = FileOps.FindEndsWith alls ".bin"
	t3 = timestamp()	
	dlls = FileOps.FindEndsWith alls ".dll"
	t4 = timestamp()	
	pngs = FileOps.FindEndsWith alls ".png"
	t5 = timestamp()	

	format "time:% (%, %, %, %) heap:%\n" (t5 - t1) (t2 - t1) (t3 - t2) (t4 - t3) (t5 - t4) (h0 - heapfree)
)
Page 2 / 2