8 Replies Latest reply on Jul 19, 2011 4:41 AM by canpserra

    [Mac OSX / JS] How to find images inside sub folders using getFiles?

    canpserra Level 1

      Hi,

      My script uses the variable myFolder to store the path for the images Folder. But when I try to use myFolder.getFiles() to retrieve images that are in sub folders the script fails. The result is null. What can I do to force myFolder.getFiles() to search inside all sub folders?

       

      var myFolder = Folder().selectDlg("Select images Folder");

      var myFiles = myFolder.getFiles();

       

      I will appreciate any help.


      Thanks,

       

      Candido

        • 1. Re: [Mac OSX / JS] How to find images inside sub folders using getFiles?
          Fred Goldman Level 3

          You need to use a recursive function and only run itself if it finds a folder.

           

          Something like this:

           

          if (myFile instanceof Folder){

          ...

          }

           

          You can use a mask with getFiles() to make sure you only get image file. Something like this:

           

          getFiles("*.jpg")

          • 2. Re: [Mac OSX / JS] How to find images inside sub folders using getFiles?
            Kasyan Servetsky Level 5

            Here is how I would do this:

            var folder = Folder.selectDialog( "Select a folder with images" );
            if ( folder != null ) {
                 var allFilesList = [];
                 GetSubFolders(folder);
                 alert("Found " + allFilesList.length + " files");
            }
            
            function GetSubFolders(theFolder) {
                 var fileList = theFolder.getFiles();
                 for (var i = 0; i < fileList.length; i++) {
                      var file = fileList[i];
                      if (file instanceof Folder){
                           GetSubFolders(file);
                      }
                      else if (file instanceof File && file.name.match(/\.(jpg|psd|tiff|pdf|eps)$/i)) {
                           allFilesList.push(file);
                      }
                 }
            }
            
            1 person found this helpful
            • 3. Re: [Mac OSX / JS] How to find images inside sub folders using getFiles?
              John Hawkinson Level 5

              Kasyan, as written that makes use of  allFilesList as a global variable in a way I find fairly confusing, and implies weird things about Javascript's scoping rules.

               

              Since Javascript doesn't have block scope, declaring the variable inside the if doesn't mean anything, and instead the variable declaration is hoisted to the top of the enclosing function, which is the global scope in this example. Then the GetSubFolders function (what happened to camelCase?) uses the fact that allFilesList is global...

              so it may not obvious to a reader the allFilesList will persist after the if() block.

               

              Maybe it's not a big deal, since it would all work fine if the whole thing were wrapped in a function... but it wouldn't work if the if block was part of a main() function and the GetSubFolders() function was not defined inside the main() function.

               

              I'd probably write this so GetSubFolders returned its value...

               

              I'm probably overthinking this.

              • 4. Re: [Mac OSX / JS] How to find images inside sub folders using getFiles?
                Kasyan Servetsky Level 5

                Hi John,

                 

                Thanks for your help.

                I'm rushing to my work  so I don't have enough time to sort it out at the moment.

                If I understand you correctly, it should be something like so:

                var files;
                var folder = Folder.selectDialog( "Select a folder with images" );
                if (folder != null) {
                     files = GetImages(folder);
                     if (files.length > 0) {
                          alert("Found " + files.length + " image files");
                     }
                     else {
                          alert("Found no image files");
                     }
                }
                
                function GetImages(theFolder) {
                     var imageFiles = [],
                     fileList = theFolder.getFiles(),
                     i, file;
                     
                     for (i = 0; i < fileList.length; i++) {
                          file = fileList[i];
                          if (file instanceof Folder){
                               GetImages(file);
                          }
                          else if (file instanceof File && file.name.match(/\.(jpg|psd|tiff|pdf|eps)$/i)) {
                               imageFiles.push(file);
                          }
                     }
                
                     return imageFiles;
                }
                

                But it doesn't work.

                 

                Regards,

                Kasyan

                • 5. Re: [Mac OSX / JS] How to find images inside sub folders using getFiles?
                  John Hawkinson Level 5

                  Yes, that's what I meant. Your implementation fails because of:

                   

                            if (file instanceof Folder){
                                 GetImages(file);
                            }

                   

                  because you throw away the return value of GetImages(). Instead you want something like

                   

                  imageFiles = imageFiles.concat(GetImages(file));
                  
                  • 6. Re: [Mac OSX / JS] How to find images inside sub folders using getFiles?
                    Kasyan Servetsky Level 5

                    Thanks for clarifying the question, John.

                    This has fixed the problem.

                     

                    Kasyan

                    • 7. Re: [Mac OSX / JS] How to find images inside sub folders using getFiles?
                      canpserra Level 1

                      Hi Kasyan,

                       

                      I heve tested your solution but now I am trying to adapt the concept to my full script. I am trying to understand every line of that code but I feel a little bit confusing. Thanks again for your help. It was very important to me.

                       

                      C. Serra

                      • 8. Re: [Mac OSX / JS] How to find images inside sub folders using getFiles?
                        canpserra Level 1

                        Hi John,

                         

                        Thanks for your comments.

                         

                        C.Serra