Titanium.Filesystem.File.isDirectory on iOS?

You must Login before you can answer or comment on any questions.

I'm trying to determine if a file object is a directory. According to the docs, the function isDirectory can only be used on Android and MobileWeb. How can I do this on iOS?

2 Answers

Accepted Answer

file.nativePath will be ended with / if it was a directory.. You can determine if a file object is a directory or not based on that characteristic :)

Here're sample patterns:

// [INFO] file://localhost/Users/minh/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/0C123CA6-4B4E-4027-B2D6-10080F9ECD86/hw.app/dir/
// [INFO] file://localhost/Users/minh/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/0C123CA6-4B4E-4027-B2D6-10080F9ECD86/hw.app/file_without_extention

— answered 1 year ago by Minh Nguyen
answer permalink
3 Comments
  • Try this sample code:

    function isDir(path) {
        var file = Ti.Filesystem.getFile(path);
     
        if ( !file.exists() ) {
            return false;
        }
     
        var nativePath = file.nativePath; 
        Ti.API.info(nativePath);
     
        return nativePath.lastIndexOf('/') == nativePath.length - 1;
    }
     
    Ti.API.info( isDir( Ti.Filesystem.getResourcesDirectory() ) );

    — commented 1 year ago by Minh Nguyen

  • Minh,

    Thank you for your insight. Great answer...

    I hope the Appcelerator team will consider making isDirectory() a universal function

    Cheers

    — commented 1 year ago by Jean-François Bohémier

  • Code strong :)

    — commented 1 year ago by Minh Nguyen

Here is a quick function for cross platform checking of directories

var isDirectory = function(file) {
    if(Ti.Platform.osname === 'ipad' || Ti.Platform.osname === 'iphone') {
        return file.nativePath.lastIndexOf('/') === file.nativePath.length-1;       
    }   
 
    return file.isDirectory();
};

Your Answer

Think you can help? Login to answer this question!