PDA

View Full Version : Search for files using mel?


AtrusDni
06-01-2007, 09:35 PM
hello, i am looking for a way to search for certain files in a project directory using mel. I have this so far:

string $searchFolder = "c:/temp";
string $stuffInFolder[] = `getFileList -fld ($searchFolder + "/")`

but that only searches for files in the immediate sub-folder. If what i was looking for was in C:\temp\folderA , then it wont find it. Anyone know how to search all sub directories in mel?

_stev_
06-02-2007, 07:35 AM
I'm not sure if there is an easier way to do it, but this will work.


global proc string[] sak_getFiles (string $dir, string $fType, string $allFiles[])
{
string $files[] = `getFileList -fld $dir`;
if (size($files) > 0)
{
string $tmp[] = `getFileList -fld $dir -fs $fType`;
appendStringArray ($allFiles, $tmp, size($tmp));
for ($f in $files)
{
if (`filetest -f ($dir + $f)` == 0)
{
sak_getFiles ($dir + $f + "\\") $fType $allFiles;
}
}
}
return $allFiles;
}

string $projectFiles[] = `sak_getFiles "C:\\Temp\\" "*.mb" $projectFiles`;


The procedure has three arguments. The folder to start in, the file type you want to find, and an empty array to put the file names in. It's recursive, so you don't want to do it on anything BIG, (like your root or program files directory). Maya will run out of memory and/or crash.

AtrusDni
06-03-2007, 04:30 PM
Thanks _stev_ for the reply! I will try this out as soon as i get home. Thank you!

AtrusDni
06-04-2007, 04:59 PM
hey _stev_ that code worked great! I have a couple questions though . . . Could you explain how the script works? I mean, I understand that you are calling the procedure and passing the file types, and location of where you want it to search. And I think you are getting the file list, and checking if there are folders in that list, right? And if there are, run the procedure again? Im kind of getting lost, sorry for my n00bish questions.

Also, is there a way to return the whole path to the file, instead of just the name?

_stev_
06-04-2007, 05:22 PM
The procedure basically gets all the files in the directory, and first checks to see which ones are subdirectories. It then sends these to the proc again. Next, it checks to see which files are of the file type you want, and adds them to the array. Since the array is sent to the proc each time, the items are continuously added to the same array, thus you end up with an array full of all the desired items.

To get the full path, you would have to add the file string to your directory path. Something like:

for ($f=0; $f<size($tmp); $f++) {
$tmp[$f] = ($dir + "\\" + $tmp[$f]);
}

I'm not in front of Maya, so I can't verify it. Try inserting this loop before the line "appendStringArray . . ."

Let me know if you need further clarification.

Stev

AtrusDni
06-04-2007, 05:40 PM
ahhh cool, thanks for you help _stev_. I will try it out now. Thanks again!

_stev_
06-05-2007, 03:49 AM
I just checked it, and had to change a few things. Put this right before the appendStringArray line:


for ($i=0; $i<size($tmp); $i++)
{
$tmp[$i] = ($dir + $tmp[$i]);
}


Changed the variable $f to an $i, because it's used later in the proc. And removed the extra \\ because the $dir variable already ends with a backslash.

AtrusDni
06-05-2007, 04:56 PM
cool ! works beautifully. Thanks for you help _stev_!

CGTalk Moderation
06-05-2007, 04:56 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.