使用cordova和apache / cordova-plugin-file插件,如何在give目录中获取超过30天的文件并将其删除?
答案 0 :(得分:2)
尝试使用此代码:
function daysDiff(now, fileDate) {
// thanks to http://stackoverflow.com/a/3224854/3340702
var timeDiff = Math.abs(now.getTime() - fileDate.getTime());
return Math.ceil(timeDiff / (1000 * 3600 * 24));
}
// Process file entries
function deleteOlderFiles(entries) {
var i;
var currentDate = new Date();
for (i=0; i<entries.length; i++) {
if(entries[i].isFile) {
entries[i].file(function(file) {
if(daysDiff(currentDate, file.lastModifiedDate) > 30) {
entries[i].remove(function(){
console.log("File removed");
}, function(){
console.log("Error while removing file");
});
}
}), error);
}
}
}
function fail(error) {
alert("Failed during operations: " + error.code);
}
// Get a directory reader
var directoryEntry = new DirectoryEntry(name, path);
var directoryReader = directoryEntry.createReader();
// Get a list of all the entries in the directory
directoryReader.readEntries(deleteOlderFiles, fail);
有关详细信息,请参阅DirectoryEntry,DirectoryReader和FileEntry文档。