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