Created
December 10, 2015 16:07
-
-
Save bcabanes/14e6d3221b841bfdf27b to your computer and use it in GitHub Desktop.
Cordova/PhoneGap: List all files entries in directories provided.
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
/** | |
* Starter vars. | |
*/ | |
var index = 0; | |
var i; | |
/** | |
* Need cordova.file plugin. | |
* $ cordova plugin add org.apache.cordova.file | |
* | |
* To get all urls see https://github.com/apache/cordova-plugin-file/blob/master/README.md#where-to-store-files | |
* | |
*/ | |
var localURLs = [ | |
cordova.file.cacheDirectory, | |
cordova.file.applicationDirectory, | |
cordova.file.applicationStorageDirectory, | |
cordova.file.dataDirectory, | |
cordova.file.documentsDirectory, | |
cordova.file.externalApplicationStorageDirectory, | |
cordova.file.externalCacheDirectory, | |
cordova.file.externalRootDirectory, | |
cordova.file.externalDataDirectory, | |
cordova.file.sharedDirectory, | |
cordova.file.syncedDataDirectory | |
]; | |
/** | |
* Recursive function for file entry. | |
*/ | |
var addFileEntry = function (entry) { | |
var dirReader = entry.createReader(); | |
dirReader.readEntries( | |
function (entries) { | |
var fileStr = ""; | |
var i; | |
for (i = 0; i < entries.length; i++) { | |
if (entries[i].isDirectory === true) { | |
// Recursive -- call back into this subdirectory | |
addFileEntry(entries[i]); | |
} else { | |
console.log(entries[i].fullPath); | |
index++; | |
} | |
} | |
}, | |
function (error) { | |
console.error("readEntries error: " + error.code); | |
} | |
); | |
}; | |
/** | |
* Directory error. | |
*/ | |
var addError = function (error) { | |
console.error("getDirectory error: " + error.code); | |
}; | |
/** | |
* Loop through the array. | |
*/ | |
for (i = 0; i < localURLs.length; i++) { | |
if (localURLs[i] === null || localURLs[i].length === 0) { | |
continue; // skip blank / non-existent paths for this platform | |
} | |
window.resolveLocalFileSystemURL(localURLs[i], addFileEntry, addError); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
readEntries() doesn't necessarily return all the entries, it's capped at 100 per call, you must call again readEntries() until the array is empty -> https://stackoverflow.com/questions/23823548/maximum-files-of-a-directory-that-can-be-read-by-filereaderreadentries-in-javas