Instance Method

enumeratorAtPath:

Returns a directory enumerator object that can be used to perform a deep enumeration of the directory at the specified path.

Declaration

- (NSDirectoryEnumerator<NSString *> *)enumeratorAtPath:(NSString *)path;

Parameters

path

The path of the directory to enumerate.

Return Value

An NSDirectoryEnumerator object that enumerates the contents of the directory at path.

If path is a filename, the method returns an enumerator object that enumerates no files—the first call to nextObject will return nil.

Discussion

Because the enumeration is deep—that is, it lists the contents of all subdirectories—this enumerator object is useful for performing actions that involve large file-system subtrees. This method does not resolve symbolic links encountered in the traversal process, nor does it recurse through them if they point to a directory.

This code fragment enumerates the subdirectories and files under a user’s Documents directory and processes all files with an extension of .doc:

NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:  @"Documents"];
NSFileManager *localFileManager=[[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum =
    [localFileManager enumeratorAtPath:docsDir];
 
NSString *file;
while ((file = [dirEnum nextObject])) {
    if ([[file pathExtension] isEqualToString: @"doc"]) {
        // process the document
        [self scanDocument: [docsDir stringByAppendingPathComponent:file]];
    }
}

The NSDirectoryEnumerator class has methods for obtaining the attributes of the existing path and of the parent directory and for skipping descendants of the existing path.

See Also

Discovering Directory Contents

contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:

Performs a shallow search of the specified directory and returns URLs for the contained items.

contentsOfDirectoryAtPath:error:

Performs a shallow search of the specified directory and returns the paths of any contained items.

enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:

Returns a directory enumerator object that can be used to perform a deep enumeration of the directory at the specified URL.

NSDirectoryEnumerator

An NSDirectoryEnumerator object enumerates the contents of a directory, returning the pathnames of all files and directories contained within that directory. These pathnames are relative to the directory.

mountedVolumeURLsIncludingResourceValuesForKeys:options:

Returns an array of URLs that identify the mounted volumes available on the device.

subpathsOfDirectoryAtPath:error:

Performs a deep enumeration of the specified directory and returns the paths of all of the contained subdirectories.

subpathsAtPath:

Returns an array of strings identifying the paths for all items in the specified directory.