7 Replies Latest reply on Jul 24, 2010 1:13 AM by Peter Kahrel

    Recursive function for getting files from folders

    sudar1983_14 Level 2

      I want to search "indd" files from my whole directory (including subdirectories).

       

       

      Arivu..

        • 1. Re: Recursive function for getting files from folders
          Loic.Aigon Adobe Community Professional

          Look into the archive, Kasyan has posted such a script, or was it Harbs, Jongware...Don't remember but the script is somewhere around.

          • 2. Re: Recursive function for getting files from folders
            Harbs. Level 6

            I think it might have been Peter Kahrel, but I don't remember for sure either...

             

            Harbs

            • 3. Re: Recursive function for getting files from folders
              Peter Kahrel Adobe Community Professional & MVP

              So it was -- the function is below. You call it like this:

               

               

               

               

               

              var myFiles = ("/d/test" );

               

              It starts searching at the specified path and includes all subdirectories. It returns an array of file objects.

               

              Peter

               

               

               

               

               

               

               

              function find_files (dir)
                  {
                  return find_files_sub (dir, []);
                  }
              
              function find_files_sub (dir, array)
                  {
                  var f = Folder (dir).getFiles ("*.*");
                  for (var i = 0; i < f.length; i++)
                      {
                      if (f[i] instanceof Folder)
                          find_files_sub (f[i], array);
                      else
                          if (f[i].name.slice (-5).toLowerCase() == ".indd")
                              array.push (f[i]);
                      }
                  return array;
                  }
              • 4. Re: Recursive function for getting files from folders
                Kasyan Servetsky Level 5

                Here is a script that illustrates how to process all subfolders in the selected folder.

                See getSubFolders function.

                 

                // Get all subfolders in the selected folder
                function getSubFolders(theFolder) {
                     var myFileList = theFolder.getFiles();
                     for (var i = 0; i < myFileList.length; i++) {
                          var myFile = myFileList[i];
                          if (myFile instanceof Folder){
                               mySubFolders.push(myFile.absoluteURI);
                               getSubFolders(myFile);
                          }
                     }
                     return mySubFolders;
                }
                

                 

                Hope this helps.

                 

                Kasyan

                1 person found this helpful
                • 5. Re: Recursive function for getting files from folders
                  Loic.Aigon Adobe Community Professional

                  Sorry Peter not to have mentioned you ;-) The only one I forgot and it was the one to te quoted :-D

                  • 6. Re: Recursive function for getting files from folders
                    sudar1983_14 Level 2

                    Thank you Peter.

                     

                    Just now i have seen your InDesign script site. It is quite good.

                     

                    http://www.kahrel.plus.com/indesignscripts.html

                     

                    Arivu..

                    • 7. Re: Recursive function for getting files from folders
                      Peter Kahrel Adobe Community Professional & MVP

                      I was blowing the dust off another file finder which I could not find earlier, and while trying that one out I discovered that getFiles ("*.*") also finds the recycle bin and everything in it. I don't know if this is a Windows-only thing or whether it affects Macs as well. To exclude the recycle bin, in the script I posted earlier, this line:

                       

                       

                      if (f[i].name.slice (-5).toLowerCase() == ".indd")

                       

                      should be replaced with this one:

                       

                      if (f[i].name.slice (-5).toLowerCase() == ".indd" && myFile.fullName.toLowerCase().indexOf ("recycle.bin") < 0)

                       

                      The same goes for Kasyan's (nice and very compact) script. Change this:

                       

                      if (myFile instanceof Folder){

                       

                      to this:

                       

                      if (myFile instanceof Folder && myFile.fullName.toLowerCase().indexOf ("recycle.bin") < 0){

                       

                      The other script I meant is a file finder in which you can use the standard OS wildcards ? (any single character) and * (any range of characters). Here are some examples ("true" is a flag for subdirectory-inclusion: "true" means "include subdirs, "false" stands for "specified dir only"):

                       

                      f = find_all ("/d/*.indd", true);

                       

                      "Find all .indd files on drive d:\"

                       

                      f = find_all ("/e/work/ch*.indd", true);

                       

                      "Find all InDesign files on in folder e:\work\ whose name starts with ch"

                       

                      f = find_all ("/d/*.ind?", true);

                       

                      "Find all InDesign-related files (.indd, .indb, .indl, etc) on drive d:\"

                       

                      f = find_all ("/e/test/*_*.jpg", true);

                       

                      "Find all jpg files in the folder e:\test\ that have an underscore in their name"

                       

                       

                      Here is the script:

                       

                       

                      #target indesign;
                      
                      f = find_all ("/d/*.indd", true);  // returns an array of file objects

                      // uncomment the next few lines to get visible output
                      /*
                      str = "Found " + f.length + " files\r" + create_string (f); if (f.length > 50)     app.documents.add().textFrames.add ({geometricBounds: ["36pt", "36pt", "806pt", "560pt"], contents: str}); else     alert (str);
                      */

                      function find_all (mask, include_sub)     {     return find_all_sub (File (mask).path+"/", [], grep_mask (File (mask).name), include_sub)     } function find_all_sub (dir, array, grep, sub)     {     var f = Folder (dir).getFiles ("*.*");     for (var i = 0; i < f.length; i++)         {         if ((f[i] instanceof Folder) && sub)             find_all_sub (f[i], array, grep, sub);         else             if (f[i].name.match (grep) != null && f[i].path.toLowerCase().indexOf ("recycle.bin") < 0)                 array.push (f[i]);         }     return array;     } // Change the OS wildcards * and ? to GREP classes function grep_mask (m)     {     // First protect any original dots     m = m.replace (/\./g, "###");     // Replace ? with . (any single character)     m = m.replace (/\?/g, ".");     // Replace * with .+? (shortest string of any character)     m = m.replace (/\*/g, ".+?");     // Restore the original dots     m = m.replace (/###/g, "\\.");     // Create GREP string: from start to end of input, case-insensitive     return RegExp ("^" + m + "$", "i");     } function create_string (file_array)     {     var list = "";     if (file_array.length > 0)         for (var i = 0; i < file_array.length; i++)             list += file_array[i].fullName + "\r"     return list     }