Search code examples
javascriptcordovafileapi

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

Solution

  • 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