Data (library)

Categories:Utilities

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

Parameters:

  • folder String The path or uri to a valid folder
  • folder Folder A valid Folder object
  • arg2 Function An optional callback of the format callback(element, index, level, indent) to call on each element. Return false to skip processing of that element
  • arg3 Number An optional max depth to recurse to, defaults to 100

Returns:

  • Array An array of paths

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>