[Closed] Search subfodlers for a file
Hey Guys,
Im writing a script to sawp a 2D VIZBlock for a 3d max file via proxy.
It does this by finding the .max file with the same name as the vizblock.
However we have .max files in several different subfolders.
Any idea how I can search a folder and it’s subfolders for a filename match?
Thanks,
Leigh
Check the “External File Methods” section in the help file. There is a sample of a recursive file finding function that should give you an idea of how to find a file.
Or you can try this:
searchSubDirs=(dotnetClass "System.IO.SearchOption")
sFiles=dotnetClass "System.IO.Directory"
theFile=(sFiles.GetFiles @"C:\2DProxy" "my2dproxy.max" searchSubDirs.AllDirectories)[1]
if theFile==undefined then print "File not found."
Artur,
Great example thanks!!! Do you know if it’s possible to find and sort filesequences with dotnet exclusivly. I have a maxscript routine, but on large directories it get’s slow. Is there a build in way for sorting files and or return file sequences?
Thanks for all these dotnet variants of script, really helpfull!!
-Johan
Yeah that works great!
just a refinement to the if statement it needs to be a double =
if theFile == undefined then print "File not found."
Hi Johan and thanks!
If you know the prefix of the file sequence then is quite easy, you just have to do something like this:
theFiles=sFiles.GetFiles @"C:\RenderSeq" "render_*.tga"
sort theFiles
I guess that should work
Yes, that assumes I know what files I want, I’m looking for a dotnet way of getting the files as an array of arrays, sorting the files by sequence. I have this in maxscript, 30+ lines of code or something and it works ok, but it can get slow on large sequences because of all the checking and sorting. So I thought maybe dotnet has a way to automagicly give me a sorted array of arrays including all sequences, just like combustion or fusion can do in the file browser. But I think even dotnet needs some help with that
Thanks though!!
-Johan
Hi Johan,
I believe it does! I had looked into this as i’ve been working on a DotNet RAM Player but i didnt find an acceptable solution with regard to file sequences. I wanted an AFX style listing of individual files and sequences, but I ended by using an identical method to Artur’s published method. VBdotnet gives you a .toarray base function method that allows you to perform a basic name sort in the way Artur’s MXS sort method does. Anything more complex you might have to implement an IComparer interface.
In the end I went with a couple of options, firstly a threaded treeview browser that performed the recursive search which was good, but could take some time on deeply nested folder structures, and finaly with an inherited directory treeview that used a temporary node structure on each folder and performed the search quickly when the node was expanded. That way, the seach logic was built into the control and you don’t need to search through a whole drive all at once. It was fast and no longer needed the background thread.
I’ll look into this!
But I’m definitely not going for a treeview but for a flatview and since I use a struct I store the results of my search there, since it’s not a constant update thing. I just want a flat overview of a folder and it’s subfolders.
And it’s pretty fast on small sequences, but slower on big ones, so I’d like to do some testing. Thanks for all the info!
-Johan