Directory Enumeration

Processing the contents of a directory is a common operation. There are a number of NSFileManager calls, which can process a directory in different ways. Both the path-based and URL-based calls will get you the contents of a directory. The URL-based calls can also prefetch the metadata you want, speeding up the operation.

Enumeration with paths

You can get a flat listing of a directory in an array:

- (NSArray *) contentsOfDirectoryAtPath: (NSString *) path 
                                  error: (NSError **) error;

This returns an array of file names of all the files in the directory. It is flat because it does not recurse into subdirectories. You can get a deep listing that recurses into subdirectories with this call:

- (NSArray *) subpathsOfDirectoryAtPath ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.