4 Replies Latest reply on Aug 8, 2017 8:39 AM by Dale_

    List subfolders in a function

    Dale_ Level 1

      Hi,

       

      I want to add a small function to a script that logs the name of every subfolder in the root folder/parent folder of the one I am working from.

       

      Here is what I have so far:

      var docRef = app.activeDocument;
      var sourceFolder = Folder (docRef.path);
      var sourceName = sourceFolder.name;
      var rootFolder= Folder (sourceFolder.path);
      var rootName = rootFolder.name;
      
      function sLog (){
          var folderName = String(rootFolder.name);
          var sub = folderName.includeSubFolders;
      
          alert (sub);
          }
      sLog();
      

       

      I want the alert to be able to list every folder.

       

      Any ideas?

        • 1. Re: List subfolders in a function
          SuperMerlin Level 5

          Something like this.

           

          #target photoshop;
          var folders =[];
          var topLevel = Folder.selectDialog("Please select top level folder");    
          folders = FindAllFolders(topLevel, folders);
          folders.unshift(topLevel);
          alert(folders.join("\n"));
          
          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;
          };
          
          
          
          
          • 2. Re: List subfolders in a function
            Dale_ Level 1

            Cheers Merlin, this result includes the full path.

             

            Any ideas on how to remove the path "~/Documents/Base/FileName" so it just shows "FileName" for the list?

            • 3. Re: List subfolders in a function
              SuperMerlin Level 5
              #target photoshop;
              var folders =[];
              var topLevel = Folder.selectDialog("Please select top level folder");    
              folders = FindAllFolders(topLevel, folders);
              folders.unshift(topLevel);
              var List = [];
              for(var a in folders){List.push(decodeURI(folders[a].name));}
              alert(List.join("\n"));
              
              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;
              };