title: Recursively populate treeview from paths in extendscript
body: <p>I am working on an extendscript function for Adobe After Effects which needs to be able to convert a list of file paths like the one below into a treeview. Here's my code so far, it walks through the first "level" fine but I am having trouble with the recursion. Help is greatly appreciated, I'm beating my head against this one and can't seem to get it to work yet!</p>
  
  <pre><code>{
      //----| Build UI
      var stImporterWin = new Window("palette", "ST Import Panel", undefined, {resizeable:true});
          stImporterWin.orientation = "column";
  
      var imageSequenceGroup = stImporterWin.add("group");
              imageSequenceGroup.alignment = ["fill", "fill"];
              imageSequenceGroup.orientation = "column";
              var imageSequenceTree = imageSequenceGroup.add("treeview");
                  imageSequenceTree.preferredSize = [600,650];
                  imageSequenceTree.alignment = ["fill", "fill"];
  
  
      stImporterWin.onResize = function () {
          this.layout.resize();
      }
  
      stImporterWin.show();
  
      filesArray = ["//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/test image2_0001.txt",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/test image3.0001.txt",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/test image_001.txt",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/0040-Major Trans M13_Logo_v02.xml",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.AO.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.MultiMatteElement.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.RGB_color.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRaySelfIllumination.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRaySpecular.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRayZDepth.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/0040-Major Trans M13_01_logo_.origRGB.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T1/Thumbs.db",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/0040-Major Trans M13_logo_02.0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/Thumbs.db",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/AO/0040-Major Trans M13_logo_02._AO0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/VRaySelfIllumination/0040-Major Trans M13_logo_02._VRaySelfIllumination0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/VRaySpecular/0040-Major Trans M13_logo_02._VRaySpecular0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Logo/T2/VRayZDepth/0040-Major Trans M13_logo_02._VRayZDepth0080.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/T1/Thumbs.db",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.AO.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.MultiMatteElement.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.RGB_color.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.VRaySelfIllumination.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.VRaySpecular.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.VRayZDepth.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/0040-Major Trans M13_01_text_.origRGB.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T1/Thumbs.db",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..AO.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..MatteText.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..RGB_color.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRaySelfIllumination.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRaySpecular.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRayZDepth.0000.png",
      "//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..origRGB.0000.png"];
  
      rootFolder = File("//VFXJobs/NY-18/02_Sequences/IntroVideo/0040-Major Trans M13/Renders");
  
      var node0 = imageSequenceTree.add("node", rootFolder.fsName.replace(/\\/g, "/"));
      node0.expanded = true;
  
      //build out arrays from the files
      for (var i = 0; i &lt; filesArray.length; i++) {
          filesArray[i] = filesArray[i].split("/");
      }
  
      //recursive tree building
      var rootAsArray = rootFolder.fsName.replace(/\\/g, "/").split("/");
      BuildChildNodes(rootAsArray, filesArray, node0, 0);
  
  
      // for debug purposes I added "killme" so that we only recurse once for now
      function BuildChildNodes (root, files, parentNode, killme) {
          alert("Root: " + root.join("/"));
          previousDirName = "";
  
          for (var i = 0; i &lt; files.length; i++) {
              var currentPath = files[i];
  
              if (currentPath.length &gt; root.length+1) {
                  // we got a sub-folder so add it to the tree if unique and recurse
                  var dirName = currentPath[root.length];
                  if (dirName != previousDirName) {
                      var newNode = parentNode.add("node", dirName);
                      newNode.expanded = true;
                      previousDirName = dirName;
  
                      // recurse
                      var newRoot = currentPath.slice(0, root.length+1);
                      var newFilesList = files.slice(i);
                      //make sure we only get this dir
                      for (var j = 0; j &lt; newFilesList.length; j++) {
                          if (newFilesList[j].slice(0, root.length+1).join("/") != newRoot.join("/")) {
                              newFilesList = newFilesList.slice(0,j);
                              break;
                          }
                      };
  
                      if (killme == 0) {
                          alert("recursing " + newRoot.join("/"));
                          BuildChildNodes(newRoot, newFilesList, newNode, 1);
                      }
  
                  }
              } else {
                  // we got a file in this dir, add it to the tree
                  var fileName = currentPath[root.length];
                  var newNode = parentNode.add("item", fileName);
              }       
          }
      }
  }
  </code></pre>
  
  <p>And the sample input list would look like:</p>
  
  <pre><code>//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/test image2_0001.txt
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/test image3.0001.txt
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/test image_001.txt
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/0040-Major Trans M13_Logo_v02.xml
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.AO.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.MultiMatteElement.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.RGB_color.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRaySelfIllumination.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRaySpecular.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.VRayZDepth.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/0040-Major Trans M13_01_logo_.origRGB.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T1/Thumbs.db
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/0040-Major Trans M13_logo_02.0080.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/Thumbs.db
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/AO/0040-Major Trans M13_logo_02._AO0080.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/VRaySelfIllumination/0040-Major Trans M13_logo_02._VRaySelfIllumination0080.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/VRaySpecular/0040-Major Trans M13_logo_02._VRaySpecular0080.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Logo/T2/VRayZDepth/0040-Major Trans M13_logo_02._VRayZDepth0080.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/T1/Thumbs.db
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.AO.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.MultiMatteElement.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.RGB_color.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.VRaySelfIllumination.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.VRaySpecular.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.VRayZDepth.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/0040-Major Trans M13_01_text_.origRGB.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T1/Thumbs.db
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..AO.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..MatteText.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..RGB_color.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRaySelfIllumination.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRaySpecular.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..VRayZDepth.0000.png
  //NY-18/02_Sequences/IntroVideo/0040-Major/Renders/Text/T2 ext/NY18_MajorTrans Ext_01_M13_text..origRGB.0000.png
  </code></pre>
  
  <p>In the end the above would look like:</p>
  
  <pre><code>//NY-18/02_Sequences/IntroVideo/0040-Major/Renders/
      test image2_0001.txt
      test image3.0001.txt
      test image_001.txt
      Logo/
          0040-Major Trans M13_Logo_v02.xml
          T1/
              0040-Major Trans M13_01_logo_.AO.0000.png
              0040-Major Trans M13_01_logo_.MultiMatteElement.0000.png
              0040-Major Trans M13_01_logo_.RGB_color.0000.png
              0040-Major Trans M13_01_logo_.VRaySelfIllumination.0000.png
              0040-Major Trans M13_01_logo_.VRaySpecular.0000.png
              0040-Major Trans M13_01_logo_.VRayZDepth.0000.png
              0040-Major Trans M13_01_logo_.origRGB.0000.png
              Thumbs.db
          T2/
              0040-Major Trans M13_logo_02.0080.png
              Thumbs.db
              AO/
                  0040-Major Trans M13_logo_02._AO0080.png
              VRaySelfIllumination/
                  0040-Major Trans M13_logo_02._VRaySelfIllumination0080.png
              VRaySpecular/
                  0040-Major Trans M13_logo_02._VRaySpecular0080.png
              VRayZDepth/
                  0040-Major Trans M13_logo_02._VRayZDepth0080.png
      T1/
          Thumbs.db
      Text/
          T1/
              0040-Major Trans M13_01_text_.AO.0000.png
              0040-Major Trans M13_01_text_.MultiMatteElement.0000.png
              0040-Major Trans M13_01_text_.RGB_color.0000.png
              0040-Major Trans M13_01_text_.VRaySelfIllumination.0000.png
              0040-Major Trans M13_01_text_.VRaySpecular.0000.png
              0040-Major Trans M13_01_text_.VRayZDepth.0000.png
              0040-Major Trans M13_01_text_.origRGB.0000.png
              Thumbs.db
          T2 ext/
              NY18_MajorTrans Ext_01_M13_text..AO.0000.png
              NY18_MajorTrans Ext_01_M13_text..MatteText.0000.png
              NY18_MajorTrans Ext_01_M13_text..RGB_color.0000.png
              NY18_MajorTrans Ext_01_M13_text..VRaySelfIllumination.0000.png
              NY18_MajorTrans Ext_01_M13_text..VRaySpecular.0000.png
              NY18_MajorTrans Ext_01_M13_text..VRayZDepth.0000.png
              NY18_MajorTrans Ext_01_M13_text..origRGB.0000.png
  </code></pre>