user3464279
user3464279

Reputation: 1

How to loop through (array, object, etc) until you are at last value - Actionscript-3

Ok, I'm having trouble on how to loop through a series of objects. There are objects within objects within objects. How could you loop through the first object and if there is an object inside that, loop through that one and so on and so forth.

Here is an example,

path = File.applicationDirectory.resolvePath(file.nativePath);

files = path.getDirectoryListing();

so files is an array of [File Object]. Array Ex. [File Object],[File Object],[File Object]

I loop through the array and get the File values I need but one of those file object is an Directory, and if it is. I want to loop through it, get the DirectoryListing and repeat until I'm at the end.

Upvotes: 0

Views: 241

Answers (1)

Pan
Pan

Reputation: 2101

As Eric said, you may use recursive, here is an example

var f : File = new File();
f.addEventListener(Event.SELECT, onFolderSelected);
f.browseForDirectory("Choose a directory");


protected function onFolderSelected(event:Event):void
{

    var targetFile:File = event.target as File;

    var files:Array = getFiles(targetFile);//the all files inthe folder

}

private function getFiles(file:File):Array
{
    var files:Array = [];

    if (file.isDirectory)
    {
        var temp:Array = file.getDirectoryListing();

        for each (var tFile:File in temp)
        {
            files = files.concat(getFiles(tFile));
        }
    }
    else
    {
        files.push(file);
    }

    return files;
}

Upvotes: 1

Related Questions