0

I'm looping through a directory of files in Cordova 3.1.0. For each entry I want the filename and the modification date.

I'm using the getMetadata method on the FileEntry object, which returns the Metadata object in the success callback but I can't see anyway to tie that Metadata object back to the FileEntry object.

This means I have an array of filenames and an array of modification dates but no link between the two.

Here's my code snippet:

// DirectoryEntry.getDirectory callback
function gotPagesDir(d)
{
    var reader = d.createReader();
    reader.readEntries(function(d){
        gotFiles(d);
        appReady();
    }, onError);
}


function gotFiles(entries)
{
    for(var i in entries)
    {
        // __CACHED_FILES is a global scoped object
        __CACHED_FILES[entries[i].name] = {name: entries[i].name};
        entries[i].getMetadata(gotMetadata, metadataError);
    }
}

function gotMetadata(metadata)
{
    var date_modified = metadata.modificationTime;
    // How do I workout which FileEntry object this metadata object belongs to?
}
Dan
  • 479
  • 6
  • 12
  • why use getMetadata() at all, it only has the date, but so does the File object. – dandavis Oct 30 '13 at 16:37
  • @dandavis Does it? The only props I can see are: filesystem, fullPath, isDirectory, isFile & name. I'm using http://cordova.apache.org/docs/en/3.1.0/cordova_file_file.md.html#FileEntry – Dan Oct 30 '13 at 16:52
  • from a FileEntry, use entry.file.lastModifiedDate... kinda dumb for them to hide the real object behind a sub-property like that, why not just merge .file with the entry? anyways, at least it's available... – dandavis Oct 30 '13 at 17:06
  • entry.file.lastModifiedDate is undefined on my device. I have to use .file() which sucks. – Alain Zelink Nov 22 '15 at 11:01

3 Answers3

1

I want to delete all the files or directory which are more than X(15) days old and faced the same challenge.I fixed it in this way. Please have a look. Hope it helps someone

metaDataCallback : function metaDataCallback (dirEntry) {
              return function(metadata) {
                var currentDate = new Date();
                if(assetservice.daysDiff(currentDate, metadata.modificationTime) > Constants.DaysForCachingAssets) {
                  dirEntry.removeRecursively(function(){
                    console.log("File removed");
                  }, function(){
                    console.log("Error while removing file");
                  });
                }
              }
        }




 for (i=0; i<entries.length; i++) {
            if(entries[i].isFile) {
              entries[i].file(function(file) {
                if(daysDiff(currentDate, file.lastModifiedDate) > 15) {
                  entries[i].remove(function(){
                    console.log("File removed");
                  }, function(){
                    console.log("Error while removing file");
                  });
                }
              }, function (error){console.log(error);});
            } else
            {
              //directory
               entries[i].getMetadata(metaDataCallback(entries[i]), function (error){console.log(error);},entries[i]);
            }
}
1

You can use .bind(), which will also work with directories, for which there is no equivalent of the callback used in the accepted answer

function gotFilesAndDirectories(entries)
{
    for(var i in entries)
    {
       entries[i].getMetadata(metadata_callback.bind({name:entries[i].name}));
    }
}

function metadata_callback(metadata)
{
    __CACHED_FILESANDDIRS[this.name] = {name: this.name, modificationTime: metadata.modificationTime};
}
Will
  • 198
  • 1
  • 6
0

In the end I followed the advice of @dandavis and used entry.file.lastModifiedDate, though even that involved using another callback.

function gotFiles(entries)
{
    for(var i in entries)
    {
       entries[i].file(file_details_callback);
    }
}

function file_details_callback(f)
{
    __CACHED_FILES[f.name] = {name: f.name, lastModifiedDate: f.lastModifiedDate};
}

Hope this helps someone else in the future

Dan
  • 479
  • 6
  • 12