Created
December 1, 2014 15:50
-
-
Save westc/dcfa7cf5cce826824c3c to your computer and use it in GitHub Desktop.
Recursively retrieve directory contents of a specified directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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