8 Replies Latest reply on Dec 29, 2011 11:20 AM by dgolberg

    Search Primary + Sub-folders for File

    dgolberg Level 1

      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.

        • 1. Re: Search Primary + Sub-folders for File
          Paul Riggott Level 6

          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[a].split(',');
          for(var z in folders){//loop through all folders to find file
              var file = File(folders[z] + "/"+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[i];
            if ( fileFoldObj instanceof File ) {   
            } else {
                   destArray.push( Folder(fileFoldObj) );
            FindAllFolders( fileFoldObj.toString(), destArray );
            }
           }
           return destArray;
          };
          main();
          
          
          • 2. Re: Search Primary + Sub-folders for File
            dgolberg Level 1

            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!

            • 3. Re: Search Primary + Sub-folders for File
              dgolberg Level 1

              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!

              • 4. Re: Search Primary + Sub-folders for File
                Paul Riggott Level 6

                Example for a csv/text file
                file.open('e');


                r (read) Opens for reading. If the file does not exist or cannot be found, the call fails.

                w (write) Opens a file for writing. If the file exists,
                its contents are destroyed. If the file does not exist,
                creates a new, empty file.

                e (edit) Opens an existing file for reading and writing.

                a (append) Opens an existing file for reading and writing,
                and moves the current position to the end of the file.

                Not all versions of Photoshop accept 'a' append

                If you use 'e' edit, after the open statement you need to move the file pointer
                to the end of the file, file.seek(0,2);position 0 from the rear of the file.

                You can write to the file with:-
                file.writeln('text'); this will write the text and add a linefeed
                file.write('text'); this will just write the text

                There isn't an easy way to edit a particular line.
                What you could to is read all the lines into an array
                then you can edit any line in the array
                Once the edits have been done, each line in the array is written back to the file.

                1 person found this helpful
                • 5. Re: Search Primary + Sub-folders for File
                  dgolberg Level 1

                  Awesome!  I'll see if I can figure out the rewriting, though I kind of doubt I'll need to use that too often (if at all) now that I've thought about it a little more.  It will be easier to simply have anyone that uses the script delete the .csv and start over. 

                   

                  I'm currently setting it up to ask the user to locate the source folder, then writing that folder's location to the .csv file (if the .csv didn't already exist) so they don't have to locate it each time.  This .csv file will also store the location of the second .csv file (which will be constantly changing depending on the project).

                   

                  I had a previous script that someone else had found and sent to me that worked with .csv files that had the following line: csvFile.open("e", "TEXT", "????"); which I was using but did not understand aside from the fact that it opened a file in append mode (and I only knew this thanks to the comment in the script).  I'm assuming the ???? in this method is how it finds the end of the file.  Going off your description above, I'd also assume this is the same as 'a' but will possibly work for older versions of Photoshop?  Anyway, no need to answer that, unless I'm wrong in that assumption (which wouldn't surprise me ).

                   

                  Thanks again for the help!  Maybe someday I'll have a real challenge for ya

                  • 6. Re: Search Primary + Sub-folders for File
                    dgolberg Level 1

                    Quick question on this.  How would you get this script to exclude a specific folder name from the search?  Apparently we have a bunch of thumbnails with the same filename that are simply in a folder called thumbnails in our main library that we need to exclude.  Searching through all of the folders as well as the thumbnail folders has quite a delay and sometimes picks the wrong file.  Also, I've got it setup to create the first CSV file and access both, and it's working wonderfully!  Thanks again for all the help!

                    • 7. Re: Search Primary + Sub-folders for File
                      Paul Riggott Level 6

                      You are getting there

                       

                      To exclude the folder named thumbnails replace the FindAllFolders function with this one...

                       

                       

                      function FindAllFolders( srcFolderStr, destArray) {
                       var fileFolderArray = Folder( srcFolderStr ).getFiles();
                       for ( var i = 0; i < fileFolderArray.length; i++ ) {
                        var fileFoldObj = fileFolderArray[i];
                        if ( fileFoldObj instanceof File ) {   
                        } else {
                            if(Folder(fileFoldObj).name.toString().toLowerCase() != 'thumbnails'){
                               destArray.push( Folder(fileFoldObj) );
                               }
                        FindAllFolders( fileFoldObj.toString(), destArray );
                        }
                       }
                       return destArray;
                      };
                      
                      
                      1 person found this helpful
                      • 8. Re: Search Primary + Sub-folders for File
                        dgolberg Level 1

                        Wow you're fast!  Glad you knew how to do that.  The method I was trying to come up with on my own was MUCH longer, lol.  Thanks again Paul!