Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if the NSURL is a directory or not

I have a NSURL object. It has address of a filesystem element, it is either a file or a directory. I want to be able to tell if the NSURL is a directory or a file.

I have already tried this, which doesn"t seem to work!

NSURL * temp ....... ;// it is initialized and has a valid value 
CFURLRef xx = (CFURLRef)CFBridgingRetain(temp);
if(CFURLHasDirectoryPath(xx)) NSLog(@"was a file");
else NSLog(@"was a folder");
like image 816
Marci-man Avatar asked Mar 09 '14 00:03

Marci-man


3 Answers

NSNumber *isDirectory;

// this method allows us to get more information about an URL. 
// We're passing NSURLIsDirectoryKey as key because that's the info we want to know.
// Also, we pass a reference to isDirectory variable, so it can be modified to have the return value
BOOL success = [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];

// If we could read the information and it's indeed a directory
if (success && [isDirectory boolValue]) {
    NSLog(@"Congratulations, it's a directory!");
} else {
    NSLog(@"It seems it's just a file.");
}
like image 107
Marcelo Fabri Avatar answered Oct 13 '22 12:10

Marcelo Fabri


With Swift 5, you can check if a URL path represents a directory or a regular file using one of the following macOS Playground sample codes.


#1. Using URL's hasDirectoryPath property

import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop")
print("is directory:", url.hasDirectoryPath)

#2. Using Filemanager's attributesOfItem(atPath:) method

import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop/File.pdf")
let attributes = try! FileManager.default.attributesOfItem(atPath: url.path)
if let type = attributes[FileAttributeKey.type] as? FileAttributeType {
    print("is file:", type == FileAttributeType.typeRegular)
}
import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop")
let attributes = try! FileManager.default.attributesOfItem(atPath: url.path)
if let type = attributes[FileAttributeKey.type] as? FileAttributeType {
    print("is directory:", type == FileAttributeType.typeDirectory)
}

#3. Using URLResourceValues

import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop")
if let resources = try? url.resourceValues(forKeys: [.isDirectoryKey]) {
    let isDirectory = resources.isDirectory ?? false
    print(isDirectory)
} else {
    print("No such file or directory")
}
import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop/File.pdf")
if let resources = try? url.resourceValues(forKeys: [.isRegularFileKey]) {
    let isFile = resources.isRegularFile ?? false
    print(isFile)
} else {
    print("No such file or directory")
}

#4. Using FileManager's fileExists(atPath:isDirectory:)

import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop")
var isDirectory: ObjCBool = false
let fileExists = FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory)
print("is directory:", fileExists && isDirectory.boolValue)
like image 40
Imanou Petit Avatar answered Oct 13 '22 11:10

Imanou Petit


Starting [iOS 9, macOS 10.11, tvOS 9.0, watchOS 2.0], there is hasDirectoryPath:

url.hasDirectoryPath
like image 38
Cœur Avatar answered Oct 13 '22 13:10

Cœur