Data (library)
Overview
Summary
The Data library offers standard functionality to iterate over various data structures. Note that the API is currently a work in progress.
Contents
API
recurseFolder(folder, arg2, arg3)
Recursively trawl a folder's contents, optionally calling a callback per element
Recurse folder provides a standard method to recurse folders, with its default functionality returning an Array of all file paths. It can also be passed a callback function in order to process each file and folder it iterates over.
Note that the order of the 2nd and 3rd arguments (depth and folder) can be swapped as they are different data types.
Default action
The following example lists the contents of the c:/temp/test folder:
var paths = Data.recurseFolder('c:/temp/test');
trace(paths.join('\n'));
c:/temp/test c:/temp/test/a folder c:/temp/test/a folder/another folder c:/temp/test/a folder/another folder/test.jsfl c:/temp/test/a folder/test.fla c:/temp/test/a folder/test.txt c:/temp/test/document.doc c:/temp/test/some file.as c:/temp/test/test copy.txt
Using a callback
The following example supplies a callback with which to process the items it iterates over:
function callback(element, index, level, indent)
{
trace (indent + '/' + element.name);
}
Data.recurseFolder('c:/temp/test/', callback);
/test
/a folder
/another folder
/test.jsfl
/test.fla
/test.txt
/document.doc
/some file.as
/test copy.txt
Skipping file or folder items
The following example shows how files and folders can be skipped by returning a Boolean false from the callback function, in this case if they have the letter "a" in their name (skipping a folder will also skip any files or folders in that folder):
function callback(element, index, level, indent)
{
return element.name.indexOf('a') == -1;
}
var paths = Data.recurseFolder('c:/temp/test/', callback);
trace(paths.join('\n'));
c:/temp/test c:/temp/test/document.doc c:/temp/test/test copy.txt
Note that the element parameter of the callback will always be a File or Folder instance, so you can access any properties of those classes.