AS3 Basics: List all files in a folder including subdirectories (AIR)

Hey all just a quickie. I’ve been building a game save backup program recently and i’ve come across several instances where i need to get a list of all the files within a directory.

so i wrote a class which does this :)

I also wrote a function which returns all the File references in a structure (again not including the directories themselves)

package com.btbStudios.air.utils
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;

	public class FileUtils
	{
		/**
		 * Lists all files in a directory structure including subdirectories, except the folders themselves.
		 *
		 * @param STARTINGFILE File the top level folder to list the contents of
		 * @param RELATIVETO File Optional If this is set all paths returned will be relative to this.
		 */
		public static function ListAllFiles(STARTINGFILE:File, RELATIVETO:File = null):String
		{
			var str:String = "";

			for each(var lstFile:File in STARTINGFILE.getDirectoryListing())
			{
				if(lstFile.isDirectory)
				{
					str+= ListAllFiles(lstFile, RELATIVETO);
				}
				else
				{
					if(RELATIVETO!=null)
					{
						str+= RELATIVETO.getRelativePath(lstFile) + "\n";
					}
					else
					{
						str+= lstFile.nativePath + "\n";
					}
				}
			}

			return str;
		}

		/**
		 * Returns an array populated with File objects representing all the files in the given directory
		 * including all the subdirectories but excluding the directory references themselves
		 *
		 * @param STARTINGFILE File the top level directory to list the contents of
		 * @param INCSUB Boolean Optional Include subdirectories
		 */
		public static function GetAllFilesFromDir(STARTINGFILE:File, INCSUB:Boolean = true):Array
		{
			var arr:Array = [];

			for each(var lstFile:File in STARTINGFILE.getDirectoryListing())
			{
				if(lstFile.isDirectory && INCSUB)
				{
					for each(var subFile:File in GetAllFilesFromDir(lstFile, true))
					{
						arr.push(subFile);
					}
				}
				else
				{
					arr.push(lstFile);
				}
			}

			return arr;
		}
}
}

Download the AS file here

Peace

-Bill

Subscribe

Subscribe to our e-mail newsletter to receive updates.

  • http://topsy.com/www.wuup.co.uk/as3-basics-list-all-files-in-a-folder-including-subdirectories-air?utm_source=pingback&utm_campaign=L2 Tweets that mention AS3 Basics: List all files in a folder including subdirectories (AIR) | Wuup — Topsy.com

    [...] This post was mentioned on Twitter by Alan Hamlyn, Paul Johnson. Paul Johnson said: List all files in a folder including subdirectories #as3 #pji #flash #air http://icio.us/5ddla1 [...]

  • Will Dignazio

    Hiya, great code, but I’m a little confused as to what the input parameters for the ListAllFiles and GetAllFilesFromDir, could you give me an example of it in use?

  • Bill Nunney

    sure :)

    var myDocuments:File = File.documentsDirectory;
    trace(FileUtils.ListAllFiles(myDocuments));

    // will dispaly a list of all files found in the “My Documents” directory

    var files:Array = FileUtils.GetAllFilesFromDir(myDocuments, true);

    // gets an array of file references to files found within “My Documents” and also includes subdirectories

  • http://mistedesign.com MisTe

    Thanks a lot! Great code, save me a lot of time ;)

  • RapsFan

    Hi. Great code, unfortunately I’m doing something wrong. I have a list and I’m trying to populate it with the contents of the Array.

    var myDocuments:File = File.documentsDirectory;
    var fu:FileUtils = new FileUtils();
    var myArray = FileUtils.GetAllFilesFromDir(myDocuments, true);

    function PopulateList(event:Event):void {

    list.addItem( myArray );

    list.selectedIndices = myArray; // This highlights song 1 by default
    gotoAndStop(3);

    }

    What am I doing wrong?

  • RapsFan

    Also tried this:

    function xmlLoaded(event:Event):void {

    for(var i:Number = 1;i< myArray.length;i++) {

    list.addItem( myArray );
    }

    list.selectedIndices = myArray; // This highlights song 1 by default
    gotoAndStop(3);

    }

  • Gronco

    hi thanks for sharing. How can I have access to arr array??
    thanks.

  • heineman99

    Very nice. Works like a charm. Thanks for sharing!

  • Mussare

    hi thanks for sharing. How can I have access to arr array??thanks.

  • heineman99

    var fileArray= GetAllFilesFromDir(STARTINGFILE, true)trace(fileArray);

  • heineman99

    var fileArray= GetAllFilesFromDir(STARTINGFILE, true)trace(fileArray);

  • Mussare

    thanks

  • Mussare

    sorry about this I’m getting this error I’m new at this :)
    :MainTimeline/ListAllFiles()[test_fla.MainTimeline::frame1:24]:MainTimeline/directorySelected()[test_fla.MainTimeline::frame1:46]

  • Mussare

    thanks I have worked it out