C ++ traverses folders and uses boost filesystem to recursively traverse folders

Source: Internet
Author: User

Compiling environment VC 9

# Ifndef scanallfiles_h
# Define scanallfiles_h
# Include "Boost/filesystem/operations. HPP"
# Include "Boost/filesystem/path. HPP"
# Include <iostream>
Using namespace STD;

Class scanallfiles {
Public:
Static const vector <string> & scanfiles (const string &, vector <string> &); // method 1. Write recursion by yourself. Use directory_iterator in filesystem
Static const vector <string> & scanfilesuserecursive (const string &, vector <string> &); // Method 2: directly use recursive_directory_iterator In the filesystem of boost
};
// Method 1: Self-writing Recursion
Const vector <string> & scanallfiles: scanfiles (const string & rootpath, vector <string> & Container = * (new vector <string> ())){
Namespace FS = boost: filesystem;
FS: path fullpath (rootpath, FS: Native );
Vector <string> & ret = container;

If (! FS: exists (fullpath) {return ret ;}
FS: directory_iterator end_iter;/** the value of the final iterator without a parameter constructor is excerpted as follows:
* If the end of the Directory elements is reached, the iterator becomes equal to the end iterator value. the constructor directory_iterator () with no arguments always constructs an end iterator object, which is the only legitimate iterator to be used for the end condition. the result of operator * on an end iterator is not defined. for any other iterator value a const directory_entry & is returned. the result ofoperator-> on an end iterator is not defined. for any other iterator value a const directory_entry * is returned.
*
**/
For (FS: directory_iterator ITER (fullpath); iter! = End_iter; ITER ++ ){
Try {
If (FS: is_directory (* ITER )){
STD: cout <* ITER <"is dir. whose parent path is" <ITER-> path (). branch_path () <STD: Endl;
Ret. push_back (ITER-> path (). String (); // push_back before Recursion
Scanallfiles: scanfiles (ITER-> path (). String (), RET); // recursively transfers the Vector
} Else {
Ret. push_back (ITER-> path (). String ());
STD: cout <* ITER <"is a file" <STD: Endl;
}
} Catch (const STD: exception & Ex ){
STD: cerr <ex. What () <STD: Endl;
Continue;
}
}
Return ret;
}
// Method 2: directly use the recursive_directory_iterator In the boost filesystem
Const vector <string> & scanallfiles: scanfilesuserecursive (const string & rootpath, vector <string> & Container = * (new vector <string> ())){
Namespace FS = boost: filesystem;
FS: path fullpath (rootpath, FS: Native );
Vector <string> & ret = container;

If (! FS: exists (fullpath) {return ret ;}
FS: recursive_directory_iterator end_iter;
For (FS: recursive_directory_iterator ITER (fullpath); iter! = End_iter; ITER ++ ){
Try {
If (FS: is_directory (* ITER )){
STD: cout <* ITER <"is dir" <STD: Endl;
Ret. push_back (ITER-> path (). String ());
// Scanallfiles: scanfiles (ITER-> path (). String (), RET );
} Else {
Ret. push_back (ITER-> path (). String ());
STD: cout <* ITER <"is a file" <STD: Endl;
}
} Catch (const STD: exception & Ex ){
STD: cerr <ex. What () <STD: Endl;
Continue;
}
}
Return ret;
}
# Endif





In addition, repost an IBM boost filesystem entry: http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/

There is also a boost filesystem Conference: http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#Class-directory_entry

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.