Cordova 检索和删除超过 30 天的文件?

Cordova retrieve and delete files older than 30 days?

使用 cordova 和 apache/cordova-plugin-file 插件,如何获取给定目录中超过 30 天的文件并将其删除?

尝试使用此代码:

function daysDiff(now, fileDate) {
    // thanks to 
    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 and FileEntry 文档。