2 Replies Latest reply on Sep 2, 2009 1:53 PM by swmangold

    Display contents of subfolders /subdirectroies

    swmangold Level 1

      Hi All,

      I'm new to AIR so maybe a dumb question.  I'm using getDirectoryListing() to get the files/folders within a given directory, but I would like to be able to list the contents within subfolders / subdirectories.  I can't seem to find any documentation or tutorials about this.  Any help greatly appreciated!!

       

      This is my code so far, launched from a mouse event:

       

      function directorySelected(event:Event):void{
           directory = event.target as File;
           var files:Array = directory.getDirectoryListing();
           fileNamesDirty.length = 0;
               for(var i:uint = 0; i < files.length; i++){
                      fileNamesDirty.push(files[i].nativePath);

                     //Works great, now how do I list files in subdirectories?!
               }
      }

       

      Once I figure out how to do this I will be able to take-over the world with my applications MUAHAHHAHHAHA!!!!!!  .. sorry it's late ...

        • 1. Re: Display contents of subfolders /subdirectroies
          alevicki

          As far as I know there is no function that will do this, but you can easily do it by cycling through your result set, although you may need to use recursion.

           

          function getFullListing(directory:File):Array{

               var files:Array = directory.getDirectoryListing();
               fileNamesDirty.length = 0;

               for(var i:uint = 0; i < files.length; i++){
                      fileNamesDirty.push(files[i].nativePath);

                      if (files[i].isDirectory){

                         fileNamesDirty.concat(getFullListing(files[i]));

                      }

               }

          }

           

          You may want to explore getDirectoryListingAsync if you are trying to traverse a large file system structure.  This will let you avoid your UI locking up while it parses the files.

          1 person found this helpful
          • 2. Re: Display contents of subfolders /subdirectroies
            swmangold Level 1

            Hi alevicki,

            Thanks for your reply, that was exactly what I was looking for!  I was able to modify your code to serve my purposes. I am now able to get a list of files and subdirectories.  I have posted the full code below for people looking for this solution.

             

             

             

            On a side note, it would be great if there was an option for "AS3 formatting" on this forum.

             

            ///////START CODE///////////

             

            //Import filesystem libraries

            import flash.filesystem.File;
            import flash.filesystem.FileMode;
            import flash.filesystem.FileStream
            ;

             

            //Define Vars / Arrays

            var directory:File = File.desktopDirectory;
            var fileList:Array = new Array();

            var files:Array = new Array();

             

            //This function gets triggered when a user selects a directory from the directory browser created by 'selectDir' function

            function getContents(event:Event):void{
                 directory = event.target as File; //Use the selected directory as the "base" directory
                 files = directory.getDirectoryListing(); //List the files and folders in the "base" directory
                 getFullListing(directory); //Run "getFullListing" to push files into the "fileList" array, and search contents of subdirectories

            }

             

            //List files and subdirectories

            function getFullListing(directory:File):void{
                 var files:Array = directory.getDirectoryListing();

                 for(var i:uint = 0; i < files.length; i++){
                        fileList.push(files[i].nativePath); //if current item in "file" array is a file, push it onto the "fileList" array
                       if (files[i].isDirectory){ //If current item is a directory..
                          fileList.concat(getFullListing(files[i])); //Run this function and add results to the end of the "fileList" array
                        }
                  }

            }

             

            //Select the directory who's contents you want to display

            function selectDir(evt:MouseEvent):void{

                  fileList.length = 0; //Clear-out the fileList array if this funtion has been run already
                  directory.browseForDirectory("Select Directory"); //Open directory browser
                  directory.addEventListener(Event.SELECT, getContents); //When directory is selected, run getContents function
            }

             

            //Pressing this button opens the directory browser

            buildItButton.addEventListener(MouseEvent.MOUSE_DOWN, selectDir);

             

            ///////END CODE///////////