Skip to content

Instantly share code, notes, and snippets.

@bcabanes
Created December 10, 2015 16:07
Show Gist options
  • Save bcabanes/14e6d3221b841bfdf27b to your computer and use it in GitHub Desktop.
Save bcabanes/14e6d3221b841bfdf27b to your computer and use it in GitHub Desktop.
Cordova/PhoneGap: List all files entries in directories provided.
/**
* 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);
}
@gianlucag
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment