I need to access every file in a folder, including file that exist within nested folders. An example folder might look like this.
animals/
-k.txt
-d.jpg
cat/
-r.txt
-z.jpg
tiger/
-a.jpg
-p.pdf
dog/
-n.txt
-f.jpg
-p.pdf
Say that I wanted to run a process on every file within "animals" that isn't folder. What would be the best way to iterate through the folder "animals" and all of its subfolders to access every file?
Thanks.
This code worked for me.
Here's a solution using
-subpathsOfDirectoryAtPath:rootPath
, with file URLs and modern Objective-C nullability bells & whistles.… and the same in Swift:
Use
NSDirectoryEnumerator
to recursively enumerate files and directories under the directory you want, and ask it to tell you whether it is a file or directory. The following is based on the example listed at the documentation for-[NSFileManager enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:]
:Here is a swift version:
Maybe you can use something like this: