Notifications
Clear all

[Closed] Get all Subdirs except some

I use a function to search files, but I don’t want to search in windows, program files and hidden folders because it is useless and not wise to do so.

Here is the code I use:

(
	excluded_folders = #("Program Files", "Program Files\\", "Program Files (x86)", "Program Files (x86)\\", "Windows", "Windows\\", "Users", "Users\\", "ProgramData", "ProgramData\\", "Documents and Settings", "Documents and Settings\\", "$WINDOWS*")
	
		fn getAllFilesInFolder root AllExisintFiles: =
			(
				dir_array = GetDirectories (root+"*")
				for d in dir_array where (findItem excluded_folders (pathConfig.stripPathToLeaf d)) == 0 do
				(	
					join dir_array (GetDirectories (d+"*"))	
					print ("Directories: " + d)
				)	
				appendIfUnique dir_array root
				for f in dir_array do
				(	
					join AllExisintFiles (getFiles (f + "*.*"))
					print ("Files: " + f)	
				)		
				AllExisintFiles
			)
			AllExisintFiles = #()
			
			getAllFilesInFolder @"C:\" AllExisintFiles:AllExisintFiles
)	

I need a faster/better way, may be using regex, C#, I don’t know. Also, a way how to determine hidden/system folders.

Please help.

5 Replies
1 Reply
(@polytools3d)
Joined: 10 months ago

Posts: 0

The biggest problem in performance is not in the method or language you use, but in the hardware. things like access time, random access time, cache, drivers, etc. can make a big difference. For instance, most SSD should perform much better than most HD. Again it all depends on the hardware, there are very slow SSD and very fast HD.

And if you scan network folders things may get a lot worse.

The function you have is not bad, and while you can get some speed improvements in C#, you may lose all this when casting the values to MXS.

You may improve it in C++, but I don’t think it can be 20 times faster if that is what you are looking for.

Try a quick test in a local HD, run it twice. Depending on the drive, the second time might be a lot faster than the first time depending on the drive cache. On the other hand, and SSD might perform very similar in both runs.

global FileOpsAssembly
fn CreateFileOpsAssembly = 
(

	source  = ""
	source += "using System;
"
	source += "using System.IO;
"
	source += "using System.Linq;
"
	source += "namespace FileOps
"
	source += "{
"
	source += "    public class Search
"
	source += "    {
"
	source += "        public string[] SearchExcept(String path, String except)
"
	source += "        {
"
	source += "            return Directory.GetFiles(path, \"*.*\", SearchOption.AllDirectories).Where(d => !d.StartsWith(except)).ToArray();
"
	source += "        }
"
	source += "    }
"
	source += "}
"

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

	compilerParams.ReferencedAssemblies.AddRange #("System.dll", "System.Core.dll")
	compilerParams.GenerateInMemory = on

	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
	compilerResults.CompiledAssembly
)

FileOpsAssembly = CreateFileOpsAssembly()
FileOps = FileOpsAssembly.CreateInstance "FileOps.Search"
/*
FileOps.SearchExcept "c:\	emp\\" "max"
*/

you can use any conditions for exception, and any their combination as well.

 lo1

Surely you’ll want that comparison to be case insensitive at least.

wait, but what about system/hidden folders, because when I search in c: I get an error

-- Error occurred in anonymous codeblock; filename: ; position: 1138; line: 33
-- Runtime error: dotNet runtime exception: Access to the path 'c:\$Recycle.Bin\S-1-5-18' is denied.

ok, it seams that I found. getFileAttribute was what I was looking for. thanks anyway

(
	clearlistener()
	excluded_folders = #("Program Files", "Program Files\\", "Program Files (x86)", "Program Files (x86)\\", "Windows", "Windows\\", "Users", "Users\\", "ProgramData", "ProgramData\\", "Documents and Settings", "Documents and Settings\\", "$WINDOWS*")
	
		fn getAllFilesInFolder root AllExisintFiles: =
			(
				dir_array = GetDirectories (root+"*")
				for d in dir_array where not (getFileAttribute d #hidden) and not (getFileAttribute d #system) and (findItem excluded_folders (pathConfig.stripPathToLeaf d)) == 0 do
				(	
					print d
					join dir_array (GetDirectories (d+"*"))	
					print ("Directories: " + d)
				)	
				/*appendIfUnique dir_array root
				for f in dir_array do
				(	
					join AllExisintFiles (getFiles (f + "*.*"))
					print ("Files: " + f)	
				)		
				AllExisintFiles*/
			)
			AllExisintFiles = #()
			
			getAllFilesInFolder @"C:\" AllExisintFiles:AllExisintFiles
)