Skip to content

Instantly share code, notes, and snippets.

@westc
Created December 1, 2014 15:50
Show Gist options
  • Select an option

  • Save westc/dcfa7cf5cce826824c3c to your computer and use it in GitHub Desktop.

Select an option

Save westc/dcfa7cf5cce826824c3c to your computer and use it in GitHub Desktop.
Recursively retrieve directory contents of a specified directory.
/**
* Recursively retrieves the contents of the specified DirectoryEntry. All
* entries are appended to an array assigned to the `entries` key of each
* DirectoryEntry.
* @param {!DirectoryEntry} dirEntry The DirectoryEntry representing the root
* directory for which all entries should be retrieved recursively.
* @param {function(Array.<DirectoryEntry,FileEntry>)} callback Function
* called after all descendant directories have been traversed.
*/
function recurseDirectory(dirEntry, callback) {
var dirsLeft = 1;
var rootEntries;
function helper(dirEntry, isRoot) {
dirEntry.createReader().readEntries(function(entries) {
dirsLeft--;
if (isRoot) {
rootEntries = entries;
}
(dirEntry.entries = entries).forEach(function(entry) {
if (entry.isDirectory) {
dirsLeft++;
helper(entry);
}
});
if (!dirsLeft) {
callback(rootEntries);
}
});
}
helper(dirEntry, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment