Instance Method

fileAttributesAtPath:traverseLink:

Returns a dictionary that describes the POSIX attributes of the file specified at a given.

Declaration

- (NSDictionary *)fileAttributesAtPath:(NSString *)path traverseLink:(BOOL)yorn;

Parameters

path

A file path.

flag

If path is not a symbolic link, this parameter has no effect. If path is a symbolic link, then:

  • If YES the attributes of the linked-to file are returned, or if the link points to a nonexistent file the method returns nil.

  • If NO, the attributes of the symbolic link are returned.

Return Value

An NSDictionary object that describes the POSIX attributes of the file specified at path. The keys in the dictionary are described in File Attribute Keys. If there is no item at path, returns nil.

Discussion

This code example gets several attributes of a file and logs them.

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *path = @"/tmp/List";
NSDictionary *fileAttributes = [fileManager fileAttributesAtPath:path traverseLink:YES];
 
if (fileAttributes != nil) {
    NSNumber *fileSize;
    NSString *fileOwner;
    NSDate *fileModDate;
    if (fileSize = [fileAttributes objectForKey:NSFileSize]) {
        NSLog(@"File size: %qi\n", [fileSize unsignedLongLongValue]);
    }
    if (fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName]) {
        NSLog(@"Owner: %@\n", fileOwner);
    }
    if (fileModDate = [fileAttributes objectForKey:NSFileModificationDate]) {
        NSLog(@"Modification date: %@\n", fileModDate);
    }
}
else {
    NSLog(@"Path (%@) is invalid.", path);
}

As a convenience, NSDictionary provides a set of methods (declared as a category in NSFileManager.h) for quickly and efficiently obtaining attribute information from the returned dictionary: fileGroupOwnerAccountName, fileModificationDate, fileOwnerAccountName, filePosixPermissions, fileSize, fileSystemFileNumber, fileSystemNumber, and fileType. For example, you could rewrite the file modification statement in the code example above as:

if (fileModDate = [fileAttributes fileModificationDate])
    NSLog(@"Modification date: %@\n", fileModDate);

Special Considerations

Because this method does not return error information, it has been deprecated as of OS X v10.5. Use attributesOfItemAtPath:error: instead.

See Also

Related Symbols

- attributesOfItemAtPath:error:

Returns the attributes of the item at a given path.

- setAttributes:ofItemAtPath:error:

Sets the attributes of the specified file or directory.