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

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

How to check if a folder exists in Cocoa & Objective-C?

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


使用NSFileManagerfileExistsAtPath:isDirectory:方法。 请在此处查看Apple的文档。


苹果公司在NSFileManager.h中提供了一些有关检查文件系统的好的建议:

"尝试操作(例如加载文件或创建目录)并优雅地处理错误要比试图提前确定操作是否成功要好得多。尝试根据操作的当前状态预测行为 文件系统或文件系统上的特定文件在面对文件系统争用情况时令人鼓舞。


[NSFileManager fileExistsAtPath:isDirectory:]

1
2
3
4
5
6
7
8
9
10
11
12
13
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.

NSFileManager是查找与文件相关的API的最佳场所。 您需要的特定API是
- fileExistsAtPath:isDirectory:

例:

1
2
3
4
5
6
7
8
9
10
11
12
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!
}


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

1
2
3
4
5
6
7
8
9
10
11
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");


推荐阅读

    linux命令进入目录6?

    linux命令进入目录6?,系统,信息,工作,命令,目录,一致,名称,位置,情况,文件,li

    linux下一层目录命令?

    linux下一层目录命令?,系统,数据,命令,目录,信息,传播,时间,基础,一致,文件,L

    linux命令进入根目录?

    linux命令进入根目录?,工作,系统,位置,设备,一致,财务,公司,地址,目录,命令,l

    linux命令去上一目录?

    linux命令去上一目录?,系统,电脑,一致,目录,上级,名称,命令,空格,读法,终端,l

    目录管理linux命令?

    目录管理linux命令?,管理,系统,工作,信息,目录,命令,文件,工具,电脑,档案,玩

    检查linux服务器命令?

    检查linux服务器命令?,系统,信息,状态,情况,时间,工具,网络,环境,服务,电脑,

    linux跳至子目录命令?

    linux跳至子目录命令?,名字,第一,第三,侄子,女婿,贪官,重典,子目录,丞相,生

    linux交换目录的命令?

    linux交换目录的命令?,系统,工作,信息,命令,标准,名称,在线,目录,分区,内容,L

    linux目录操作命令d?

    linux目录操作命令d?,工作,系统,信息,命令,情况,基础,数据,名称,地址,目录,li

    linux访问根目录命令?

    linux访问根目录命令?,系统,位置,一致,公司,财务,设备,命令,根目录,传播,密

    linux检查硬盘的命令?

    linux检查硬盘的命令?,系统,信息,检测,情况,命令,工具,电脑,地址,设备,硬盘,l

    linux上层目录命令?

    linux上层目录命令?,系统,一致,网络,命令,目录,管理,数据,电脑,空格,文件,Lin

    linux如何目录命令?

    linux如何目录命令?,名称,首次,系统,名字,数据,命令,目录,信息,标准,文件,Lin

    linux命令是否存在?

    linux命令是否存在?,单位,系统,信息,工具,网络,权威,状态,时间,官网,命令,测

    linux命令是否存在?

    linux命令是否存在?,单位,系统,信息,工具,网络,权威,状态,时间,官网,命令,测

    linux命令回上层目录?

    linux命令回上层目录?,电脑,系统,一致,服务,目录,命令,上级,名称,空格,终端,l

    linux基本目录命令?

    linux基本目录命令?,系统,工作,地址,信息,命令,管理,目录,操作,功能,控制台,l

    linux检查挂载命令?

    linux检查挂载命令?,设备,系统,信息,情况,状态,服务,软件,命令,磁盘,网络,lin

    linux命令新增目录?

    linux命令新增目录?,名字,首次,名称,新增,文件,命令,密码,软件,流程,系统,lin

    linux网站根目录命令?

    linux网站根目录命令?,系统,一致,设备,网站,目录,根目录,信息,标准,位置,电