Skip to main content
Inspiring
December 23, 2011
Answered

Search Primary + Sub-folders for File

  • December 23, 2011
  • 1 reply
  • 1479 views

Ok, I know how to locate a specific file in a specific location; but now I need a way to locate a specific file in a general location.  Basically, I have a library of textures with their exact file names.  Thing is, I don't always know their exact location.  For example, say I need to run the textures "brushed_aluminum.jpg" through my script, but I don't know exactly where it is, I just know it's somewhere in my textures folder (c:\textures\  for simplicity sake).  Now, within c:\textures\  there are multiple sub-folders (and possibly sub-sub-folders, etc.) that it could be in.  How would I go about having the script find the file within this general location, then get it's exact path and store the exact path + filename to a variable?  Thanks for any help!

dgolberg

Edit: I had 2 other questions, but I was able to solve them myself.

This topic has been closed for replies.
Correct answer Paul Riggott

Adding to the last example...

function main(){

var Path = File($.fileName).parent;

var csvFile = File(Path + "/csvFile.csv");//csv file

var errorLog = File(Path + "/errors.txt");//error log

$.os.search(/windows/i)  != -1 ? errorLog.lineFeed = 'windows'  : errorLog.lineFeed = 'macintosh';

if(!csvFile.exists){

    alert("Unable to find CSV file!");

    return;

    }

var sourceFolder = Folder.selectDialog("Please select Folder where files are to be found");

if(sourceFolder == null) return;

var folders =[];

folders = FindAllFolders(sourceFolder, folders); //get a list of all sub folders

folders.unshift(sourceFolder);

csvFile.open('r');

var data=[];

csvFile.readln(); //do not process the header line

while(!csvFile.eof){

   var line = csvFile.readln().replace(/^\s+|\s+$/g, ''); //remove leading and trailing spaces

   if(line.length>3) data.push(line); //Make sure it's not a blank line

}

csvFile.close();

for(var a in data){//process your files

var bits = data.split(',');

for(var z in folders){//loop through all folders to find file

    var file = File(folders + "/"+bits[0]);

    if(file.exists) break;

    }

if(!file.exists) {//if file not found it is logged in error file.

    errorLog.open('e');

    errorLog.seek(0,2);

    errorLog.writeln(bits[0] + " could not be found");

    errorLog.close();

    continue;

    }

    //open (file);

    //runaction

    //app.doAction(bits[1].toString(), bits[2].toString());

    //do whatever

    //save and close you document

    alert(file +"\r will be running action - " + bits[1].toString() + " actionSet = " + bits[2].toString() );

    }

}

function FindAllFolders( srcFolderStr, destArray) {

var fileFolderArray = Folder( srcFolderStr ).getFiles();

for ( var i = 0; i < fileFolderArray.length; i++ ) {

  var fileFoldObj = fileFolderArray;

  if ( fileFoldObj instanceof File ) {  

  } else {

         destArray.push( Folder(fileFoldObj) );

  FindAllFolders( fileFoldObj.toString(), destArray );

  }

}

return destArray;

};

main();

Paul Riggott
Paul RiggottCorrect answer
Inspiring
December 23, 2011

Adding to the last example...

function main(){

var Path = File($.fileName).parent;

var csvFile = File(Path + "/csvFile.csv");//csv file

var errorLog = File(Path + "/errors.txt");//error log

$.os.search(/windows/i)  != -1 ? errorLog.lineFeed = 'windows'  : errorLog.lineFeed = 'macintosh';

if(!csvFile.exists){

    alert("Unable to find CSV file!");

    return;

    }

var sourceFolder = Folder.selectDialog("Please select Folder where files are to be found");

if(sourceFolder == null) return;

var folders =[];

folders = FindAllFolders(sourceFolder, folders); //get a list of all sub folders

folders.unshift(sourceFolder);

csvFile.open('r');

var data=[];

csvFile.readln(); //do not process the header line

while(!csvFile.eof){

   var line = csvFile.readln().replace(/^\s+|\s+$/g, ''); //remove leading and trailing spaces

   if(line.length>3) data.push(line); //Make sure it's not a blank line

}

csvFile.close();

for(var a in data){//process your files

var bits = data.split(',');

for(var z in folders){//loop through all folders to find file

    var file = File(folders + "/"+bits[0]);

    if(file.exists) break;

    }

if(!file.exists) {//if file not found it is logged in error file.

    errorLog.open('e');

    errorLog.seek(0,2);

    errorLog.writeln(bits[0] + " could not be found");

    errorLog.close();

    continue;

    }

    //open (file);

    //runaction

    //app.doAction(bits[1].toString(), bits[2].toString());

    //do whatever

    //save and close you document

    alert(file +"\r will be running action - " + bits[1].toString() + " actionSet = " + bits[2].toString() );

    }

}

function FindAllFolders( srcFolderStr, destArray) {

var fileFolderArray = Folder( srcFolderStr ).getFiles();

for ( var i = 0; i < fileFolderArray.length; i++ ) {

  var fileFoldObj = fileFolderArray;

  if ( fileFoldObj instanceof File ) {  

  } else {

         destArray.push( Folder(fileFoldObj) );

  FindAllFolders( fileFoldObj.toString(), destArray );

  }

}

return destArray;

};

main();

dgolbergAuthor
Inspiring
December 27, 2011

Worked perfectly right out of the gate!  Thanks a ton Paul!  Sorry for the delay in responding, by the way.  Was caught up in the holiday spirit

Now I'll just need to make a couple minor modifications (such as the .csv name etc.) to fit it to what I'm doing and I'll be good to go!

Thanks again for the help!

dgolbergAuthor
Inspiring
December 27, 2011

I guess I have a couple quick questions regarding CSV files.  Is there a way to write/read data to/from a specific line in the file?  Currently, I only know of the csvFile.writeln(); method for writting data in or csvFile.readln(); for reading it.  Also, on the csvFile.open('r'); what does the 'r' part do?  I'm not very familiar with what these do to modify how it's opened.  Is there a list of these (I'm assuming their settings) somewhere?  Thanks again!