Enumerate the contents of a directory using Nsfilemanager (Traverse directory)

Source: Internet
Author: User

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:

  1. #import <Foundation/Foundation.h>   
  2. &NBSP;&NBSP;
  3. int  main ( int &NBSP;ARGC,&NBSP; const &NBSP; char  * argv[])   
  4. {
  5. @autoreleasepool {
  6. NSString *path;
  7. Nsfilemanager *FM;
  8. Nsdirectoryenumerator *direnum;
  9. Nsarray *dirarray;
  10. FM = [Nsfilemanager Defaultmanager];
  11. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
  12.         //Gets the path to the current working directory &NBSP;&NBSP;
  13. PATH = [FM Currentdirectorypath];
  14.         //The first method of traversing this directory: (deep traversal, recursively enumerating its contents) &NBSP;&NBSP;
  15. DIRENUM = [FM Enumeratoratpath:path];
  16.         nslog (@" 1.contents of %@: " ,path);   
  17.         while   (path = [direnum  nextobject])  != nil)   
  18. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;
  19.             nslog (@"%@ " ,path);   
  20. }
  21.         //Another way to traverse the directory: (Do not recursively enumerate the contents of the folder type) &NBSP;&NBSP;
  22.         dirarray = [fm  directorycontentsatpath:[fm currentdirectorypath]];  
  23.         nslog (@" 2.contents using directorycontentsatpath: "
  24. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
  25.         for
  26.             nslog (@"%@ " ,path);      
  27. }
  28. return  0;
  29. }

Enumerate the contents of a directory using Nsfilemanager (Traverse directory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.