如何检查Cocoa & Objective-C中是否存在文件夹?

时间:2021-07-19 00:04:44

How to check if a folder (directory) exists in Cocoa using Objective-C?

如何使用Objective-C检查Cocoa中是否存在文件夹(目录)?

5 个解决方案

#1


70  

Use NSFileManager's fileExistsAtPath:isDirectory: method. See Apple's docs here.

使用NSFileManager fileExistsAtPath:isDirectory:方法。看到苹果的文档。

#2


13  

Some good advice from Apple in the NSFileManager.h regarding checking the file system:

苹果在NSFileManager上的一些好建议。h检查文件系统:

"It's far better to attempt an operation (like loading a file or creating a directory) and handle the error gracefully than it is to try to figure out ahead of time whether the operation will succeed. Attempting to predicate behavior based on the current state of the filesystem or a particular file on the filesystem is encouraging odd behavior in the face of filesystem race conditions."

尝试一个操作(比如加载一个文件或创建一个目录)并优雅地处理错误,要比事先判断操作是否成功要好得多。试图根据文件系统当前状态或文件系统上的特定文件来断言行为,这在文件系统竞态条件下鼓励了奇怪的行为。

#3


10  

[NSFileManager fileExistsAtPath:isDirectory:]

【NSFileManager fileExistsAtPath:isDirectory:】

Returns a Boolean value that indicates whether a specified file exists.

- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory

Parameters
path
The path of a file or directory. If path begins with a tilde (~), it must first be expanded with stringByExpandingTildeInPath, or this method will return NO.

isDirectory
Upon return, contains YES if path is a directory or if the final path element is a symbolic link that points to a directory, otherwise contains NO. If path doesn’t exist, the return value is undefined. Pass NULL if you do not need this information.

Return Value
YES if there is a file or directory at path, otherwise NO. If path specifies a symbolic link, this method traverses the link and returns YES or NO based on the existence of the file or directory at the link destination.

#4


7  

NSFileManager is the best place to look for file related APIs. The specific API you require is - fileExistsAtPath:isDirectory:.

NSFileManager是查找文件相关api的最佳位置。您需要的特定API是- fileExistsAtPath:isDirectory:。

Example:

例子:

NSString *pathToFile = @"...";
BOOL isDir = NO;
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:pathToFile isDirectory:&isDir];

if(isFile)
{
    //it is a file, process it here how ever you like, check isDir to see if its a directory 
}
else
{
    //not a file, this is an error, handle it!
}

#5


1  

If your have a NSURL object as path, it's better to use path to convert it into NSString.

如果有一个NSURL对象作为路径,最好使用path将其转换为NSString。

NSFileManager*fm = [NSFileManager defaultManager];

NSURL* path = [[[fm URLsForDirectory:NSDocumentDirectory 
                           inDomains:NSUserDomainMask] objectAtIndex:0]                           
                                 URLByAppendingPathComponent:@"photos"];

NSError *theError = nil;
if(![fm fileExistsAtPath:[path path]]){
    NSLog(@"dir doesn't exists");
}else
    NSLog(@"dir exists");

#1


70  

Use NSFileManager's fileExistsAtPath:isDirectory: method. See Apple's docs here.

使用NSFileManager fileExistsAtPath:isDirectory:方法。看到苹果的文档。

#2


13  

Some good advice from Apple in the NSFileManager.h regarding checking the file system:

苹果在NSFileManager上的一些好建议。h检查文件系统:

"It's far better to attempt an operation (like loading a file or creating a directory) and handle the error gracefully than it is to try to figure out ahead of time whether the operation will succeed. Attempting to predicate behavior based on the current state of the filesystem or a particular file on the filesystem is encouraging odd behavior in the face of filesystem race conditions."

尝试一个操作(比如加载一个文件或创建一个目录)并优雅地处理错误,要比事先判断操作是否成功要好得多。试图根据文件系统当前状态或文件系统上的特定文件来断言行为,这在文件系统竞态条件下鼓励了奇怪的行为。

#3


10  

[NSFileManager fileExistsAtPath:isDirectory:]

【NSFileManager fileExistsAtPath:isDirectory:】

Returns a Boolean value that indicates whether a specified file exists.

- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory

Parameters
path
The path of a file or directory. If path begins with a tilde (~), it must first be expanded with stringByExpandingTildeInPath, or this method will return NO.

isDirectory
Upon return, contains YES if path is a directory or if the final path element is a symbolic link that points to a directory, otherwise contains NO. If path doesn’t exist, the return value is undefined. Pass NULL if you do not need this information.

Return Value
YES if there is a file or directory at path, otherwise NO. If path specifies a symbolic link, this method traverses the link and returns YES or NO based on the existence of the file or directory at the link destination.

#4


7  

NSFileManager is the best place to look for file related APIs. The specific API you require is - fileExistsAtPath:isDirectory:.

NSFileManager是查找文件相关api的最佳位置。您需要的特定API是- fileExistsAtPath:isDirectory:。

Example:

例子:

NSString *pathToFile = @"...";
BOOL isDir = NO;
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:pathToFile isDirectory:&isDir];

if(isFile)
{
    //it is a file, process it here how ever you like, check isDir to see if its a directory 
}
else
{
    //not a file, this is an error, handle it!
}

#5


1  

If your have a NSURL object as path, it's better to use path to convert it into NSString.

如果有一个NSURL对象作为路径,最好使用path将其转换为NSString。

NSFileManager*fm = [NSFileManager defaultManager];

NSURL* path = [[[fm URLsForDirectory:NSDocumentDirectory 
                           inDomains:NSUserDomainMask] objectAtIndex:0]                           
                                 URLByAppendingPathComponent:@"photos"];

NSError *theError = nil;
if(![fm fileExistsAtPath:[path path]]){
    NSLog(@"dir doesn't exists");
}else
    NSLog(@"dir exists");