1 Reply Latest reply on Nov 9, 2010 3:08 AM by celebritymusic

    Listing a subdir's contents?

    celebritymusic Level 1

      Hi

      I'm wondering how to change the following code so that I get a listing of files in a folder called 'tester' that resides in the
      documentsDirectory?

      var directory:File = File.documentsDirectory;
      directory.getDirectoryListingAsync();
      directory.addEventListener(FileListEvent.DIRECTORY_LISTING, directoryListingHandler);

      function directoryListingHandler(event:FileListEvent):void {
      var list:Array = event.files;
      for (var i:uint = 0; i < list.length; i++) {
      trace(list[i].nativePath);
      }
      }


      I found another set of code that's close, but I still can't get it to work?

      Is there a simple way to add /tester to the abvove code?

      var fileList = new Array();

      function getFilesRecursive(folder) {
      //the current folder object
      var currentFolder = new File(folder);

      //the current folder's file listing
      var files = currentFolder.getDirectoryListing();

      //iterate and put files in the result and process the sub folders recursively
      for (var f = 0; f < files.length; f++) {
      if (files[f].isDirectory) {
      if (files[f].name !="." && files[f].name !="..") {
      //it's a directory
      getFilesRecursive(files[f].nativePath);
      }
      } else {
      //it's a file yupeee
      fileList.push(files[f].nativePath);
      }
      }           

      }

        • 1. Re: Listing a subdir's contents?
          celebritymusic Level 1

          OK - for anyone that may need it - I played a bit and got it to work:

           

          var mediaDirList:File = File.documentsDirectory;
          mediaDirList = mediaDirList.resolvePath("tester/");
          mediaDirList.getDirectoryListingAsync();
          mediaDirList.addEventListener(FileListEvent.DIRECTORY_LISTING, directoryListingHandler);


          function directoryListingHandler(event:FileListEvent):void {
              var list:Array = event.files;
              for (var i:uint = 0; i < list.length; i++) {
                  trace(list[i].nativePath);
              }
          }

           

          This seems WAYYY simpler than some of the solutions I looked at, so I'm wondering if it's best practice - it would work cross platform, right?

           

          Thanks (forgot my manners in the last post).

           

          Shaun