Instance Method

fileExistsAtPath:isDirectory:

Returns a Boolean value that indicates whether a file or directory exists at a specified path. The isDirectory out parameter indicates whether the path points to a directory or a regular file.

Declaration

- (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, this value is undefined upon return. Pass NULL if you do not need this information.

Return Value

YES if a file at the specified path exists, or NO if the file’s does not exist or its existence could not be determined.

Discussion

If the file at path is inaccessible to your app, perhaps because one or more parent directories are inaccessible, this method returns NO. If the final element in path specifies a symbolic link, this method traverses the link and returns YES or NO based on the existence of the file at the link destination.

If you need to further determine whether path is a package, use the isFilePackageAtPath: method of NSWorkspace.

This example gets an array that identifies the fonts in the user's fonts directory:

NSArray *subpaths;
BOOL isDir;
 
NSArray *paths = NSSearchPathForDirectoriesInDomains
                     (NSLibraryDirectory, NSUserDomainMask, YES);
 
if ([paths count] == 1) {
 
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSString *fontPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Fonts"];
 
    if ([fileManager fileExistsAtPath:fontPath isDirectory:&isDir] && isDir) {
        subpaths = [fileManager subpathsAtPath:fontPath];
// ...

See Also

Determining Access to Files

fileExistsAtPath:

Returns a Boolean value that indicates whether a file or directory exists at a specified path.

isReadableFileAtPath:

Returns a Boolean value that indicates whether the invoking object appears able to read a specified file.

isWritableFileAtPath:

Returns a Boolean value that indicates whether the invoking object appears able to write to a specified file.

isExecutableFileAtPath:

Returns a Boolean value that indicates whether the operating system appears able to execute a specified file.

isDeletableFileAtPath:

Returns a Boolean value that indicates whether the invoking object appears able to delete a specified file.