Question

I am working on an iPad app using appcelarator titanium and need to iterate over the content of a directory and determine the type of the contained items, whether it is a file or directory.

This is what I have so far:

dirFullPath = '/full/path/to/directory';
var dir = Titanium.Filesystem.getFile(dirFullPath);
var dirItems = dir.getDirectoryListing();
for ( var i=0; i<dir.length; i++ ) {
    itemFullPath = dirFullPath
                 + Titanium.Filesystem.getSeparator()
                 + dir[i].toString();
    testItem = Titanium.Filesystem.getFile(itemFullPath);
    if ( testItem.exists() ) {
        alert(itemFullPath + ' exists.');             // item exists, alert box appears
        if ( testItem.isDirectory ) {            
            alert(itemFullPath + ' is a directory.'); // this code is never executed
        }
        else if ( testItem.isFile ) {
            alert(itemFullPath + ' is a file.');      // this code is never executed
        }
        else {
            alert(itemFullPath + ' is an unknown object.'); // this alert is always shown
        }
    }
}

I always get the alert box saying " is an unknown object.". It seems, that isFile and isDirectory are not working properly, or did I miss something? Does anybody else had the same problem?

Thanks for any advice!

Was it helpful?

Solution

The following should work:

var isDirectory = function(f){
    return f.exists() && f.getDirectoryListing() != null;
}

var isFile = function(f){
    return f.exists() && f.getDirectoryListing() == null;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top