5 Replies Latest reply on Oct 28, 2009 5:31 AM by G. Singelmann

    [JS] Can I get Children Folders of a folder ?

    Loic.Aigon Adobe Community Professional

      Hi,

       

      Meanwhile there is a Folder.getFiles() command, I didn't find any Folder.getFolders() or Folder.folders or Folder.children in the ESTK DOM.

       

      The only way I see is to write this specific operation both in VB and AS and call them via a doScript depending on the system.

      But before doing this, I would like to be sure JS couldn't help me.

       

      Thanks for advices.

       

      Loic

        • 1. Re: [JS] Can I get Children Folders of a folder ?
          Kasyan Servetsky Level 5

           

          Hi Loic,

           

           

           

          Here is how I solved this in my 'Place images script' — http://kasyan.ho.com.ua/place_images.html

           

           

          // global variable (empty array)

          var mySubFolders = [];

           

          // myStartFolder is the folder from which to start collecting subfolders

          mySubFolders = getSubFolders(myStartFolder);

           

           

           

          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

          • 2. Re: [JS] Can I get Children Folders of a folder ?
            Loic.Aigon Adobe Community Professional

            Hi Kasyan,

             

            Thanks so much for your input.

            It does work so well.You are a killer.

             

            Regards,

             

            Loic

            • 3. Re: [JS] Can I get Children Folders of a folder ?
              G. Singelmann Level 3

              The getFile function takes a mask parameter which can also be a function.

               

              Thus you can filter the folders:

               

              var f = Folder.selectDialog ("Which Folder?");

              var sf = f.getFiles(onlyFolders);

              var s = "";

              for (var n = 0; n < sf.length; n++) {

                s += sf[n].name + "\r";

              }

              alert(s);

               

              function onlyFolders(f) {

                if (f.constructor.name == "File") {

                  return false;

                } else {

                  return true;

                }

              1 person found this helpful
              • 4. Re: [JS] Can I get Children Folders of a folder ?
                Loic.Aigon Adobe Community Professional

                Hi and thanks a lot for your input too.

                 

                In fact, I thought getFiles could only return File objects.

                Perfect.

                 

                I was finding very tricky to write applescript and visualbasic just for that goal !

                 

                Regards,

                 

                Loic

                 

                PS: This forum is gold ! And you are the nuggets :-)

                • 5. Re: [JS] Can I get Children Folders of a folder ?
                  G. Singelmann Level 3

                  >I thought getFiles could only return File objects.

                   

                  The name insinuates so. That's why it's so important to read the OMV carefully:

                   

                  mask: Data Type: any

                  A search mask for file names, specified as a string or a function.

                  A mask string can contain question mark (?) and asterisk (*) wild cards. Default is "*", which matches all file names.

                  Can also be the name of a function that takes a File or Folder object as its argument. It is called for each file or folder found in the search; if it returns true, the object is added to the return array.

                  NOTE: In Windows, all aliases end with the extension .lnk. ExtendScript strips this from the file name when found, in order to preserve compatibility with other operating systems. You can search for all aliases by supplying the search mask "*.lnk", but note that such code is not portable.