Following is a few lines of Objective-C code to quickly traverse a directory and all of its subdirectories.
Let’s start with a file structure in the Documents directory that I used for my example:
The example I am writing is using the simulator, thus the folder structure shown above is from my Mac. However, the same applies regardless of the platform.
// Create file manager
fileMgr = [NSFileManager defaultManager];
// Path to documents directory
documentsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Change to Documents directory
// Enumerator for docs directory
enumerator = [fileMgr enumeratorAtPath:documentsDir];
// Get each entry (file or folder)
while ((entry = [enumerator nextObject]) != nil)
// File or directory
if ([fileMgr fileExistsAtPath:entry isDirectory:&isDirectory] && isDirectory)
NSLog (@"Directory - %@", entry);
NSLog (@" File - %@", entry);
Line 17 is the most relevant line of code, wherein the enumerator manages traversing the current directory and all subdirectories. From there I check whether or not the object returned is a file or directory and output the results.