Dan
Dan

Reputation: 489

Metadata in Cordova's File API

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?
}

Upvotes: 0

Views: 2063

Answers (3)

Will
Will

Reputation: 369

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};
}

Upvotes: 1

dheeraj chugh
dheeraj chugh

Reputation: 11

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]);
            }
}

Upvotes: 1

Dan
Dan

Reputation: 489

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

Upvotes: 0

Related Questions