You need to get a list of the contents of the directory, using the Enumeratoratpath: Method or Directoryc Ontentsatpath: method, you can complete the enumeration process.
If you use the first Enumeratoratpath: method, you can enumerate each file in the specified directory at one time. By default, if one of the files is a directory, its contents are also recursively enumerated. In this process, the recursive process can be dynamically blocked by sending an skipdescendants message to the enumeration object, so that the contents of the directory are no longer enumerated.
For the Directorycontentsatpath: method, use this method to enumerate the contents of the specified directory and return the list of files in an array. If any file in this directory is itself a directory, this method does not recursively enumerate its contents.
The code is as follows:
- #import <Foundation/Foundation.h>
- &NBSP;&NBSP;
- int main ( int &NBSP;ARGC,&NBSP; const &NBSP; char * argv[])
- {
- @autoreleasepool {
- NSString *path;
- Nsfilemanager *FM;
- Nsdirectoryenumerator *direnum;
- Nsarray *dirarray;
- FM = [Nsfilemanager Defaultmanager];
- &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
- //Gets the path to the current working directory &NBSP;&NBSP;
- PATH = [FM Currentdirectorypath];
- //The first method of traversing this directory: (deep traversal, recursively enumerating its contents) &NBSP;&NBSP;
- DIRENUM = [FM Enumeratoratpath:path];
- nslog (@" 1.contents of %@: " ,path);
- while (path = [direnum nextobject]) != nil)
- &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;
- nslog (@"%@ " ,path);
- }
- //Another way to traverse the directory: (Do not recursively enumerate the contents of the folder type) &NBSP;&NBSP;
- dirarray = [fm directorycontentsatpath:[fm currentdirectorypath]];
- nslog (@" 2.contents using directorycontentsatpath: "
- &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
- for
- nslog (@"%@ " ,path);
- }
- return 0;
- }
Enumerate the contents of a directory using Nsfilemanager (Traverse directory)