[Closed] Faster way to count all files and directories in specific path?
I am trying to count all files and directories in specific path with a faster way,there is already a solution with c# code:
private static long SizeOf(string directory)
{
var fcounter = new CSharpTest.Net.IO.FindFile(directory, "*", true, true, true);
fcounter.RaiseOnAccessDenied = false;
long size = 0, total = 0;
fcounter.FileFound +=
(o, e) =>
{
if (!e.IsDirectory)
{
Interlocked.Increment(ref total);
size += e.Length;
}
};
Stopwatch sw = Stopwatch.StartNew();
fcounter.Find();
Console.WriteLine("Enumerated {0:n0} files totaling {1:n0} bytes in {2:n3} seconds.",
total, size, sw.Elapsed.TotalSeconds);
return size;
}
The question is,is there a same solution in maxscript?
Actually I’ve build the c# source to a dll file from here to try to work:
http://help.csharptest.net/?CSharpTest.Net.Library~CSharpTest.Net.IO.FindFile_members.html
Unfortunately I stucked,I don’t know what’s the means,does anyone can help?Thanks in advance.
DotNet.LoadAssembly @"C:\CSharp.dll"
a=dotnetclass "CSharpTest.Net.IO.FindFile"
dotNetClass:CSharpTest.Net.IO.FindFile
showmethods a
.[static]AllFilesAndFoldersIn <System.String>directory <System.Action`1[[CSharpTest.Net.IO.FindFile+FileFoundEventArgs, ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]>e
.[static]AllFilesIn <System.String>directory <System.Action`1[[CSharpTest.Net.IO.FindFile+FileFoundEventArgs, ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]>e
.[static]AllFoldersIn <System.String>directory <System.Action`1[[CSharpTest.Net.IO.FindFile+FileFoundEventArgs, ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]>e
.[static]<System.Boolean>Equals <System.Object>objA <System.Object>objB
.[static]FilesAndFoldersIn <System.String>directory <System.Action`1[[CSharpTest.Net.IO.FindFile+FileFoundEventArgs, ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]>e
.[static]FilesIn <System.String>directory <System.Action`1[[CSharpTest.Net.IO.FindFile+FileFoundEventArgs, ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]>e
.[static]FoldersIn <System.String>directory <System.Action`1[[CSharpTest.Net.IO.FindFile+FileFoundEventArgs, ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]>e
.[static]<System.Boolean>ReferenceEquals <System.Object>objA <System.Object>objB
a.AllFilesAndFoldersIn @"C:\" <FileFoundEventArgs> --how to use <FileFoundEventArgs>?
Refrence help
http://help.csharptest.net/CSharpTest.Net.Library~CSharpTest.Net.IO.FindFile+FileFoundEventArgs.html
mxs is more efficient for this , most time , not all time
(
st = timestamp()
yourpath = @"....."
sel=#(#(),#())
fn getFilesRecursive p =
(
allpath = GetDirectories (p+"/*")
join sel[1] allpath
join sel[2] (getFiles (p +"/*.*" ))
for i in allpath do
getFilesRecursive i
)
getFilesRecursive yourpath
format " %ms %dirs %files\n" (timestamp()-st) sel[1].count sel[2].count
)
dotnet can get all path and files , it is a Double-edged sword , some paths are denied to access , such as “System Volume Information” , try to get files in it will cause runtime error, you can use try to avoid ,but more slow
if the path is a very large path such as root “c:” , dotnet faster than mxs
(
st = timestamp()
cl=dotnetclass "System.IO.Directory"
yourpath = @"....."
sel=#(#(),#())
fn getallfiles p =
(
allpath = cl.GetDirectories p
join sel[1] allpath
join sel[2] (cl.getFiles p)
for i in allpath do
getFilesRecursive i
)
getallfiles yourpath
format " %ms %dirs %files\n" (timestamp()-st) sel[1].count sel[2].count
)
if just get count , change sel to #(0,0) ,and change the function “join” lines to sel[1]+=allpath.count and sel[2]+= (…).count
by @Swordslayer
The getFiles() and getDirectories() methods have a recurse: parameter to indicate whether to recursively search for files or directories.