One of the most convenient features is to traverse all the content in the path. Directory_iterator. Path P;
Directory_iterator (P) is the starting point of the iterator, and Directory_iterator () without parameters is the end point of the iterator.
Get all the file names under the directory
#include <boost/filesystem.hpp> namespace fs = boost::filesystem; Int get_filenames (const std::string& dir, std::vector<std::string >& filenames) { fs::p ath path (dir); if (!fs::exists (path)) { return -1; } fs::d irectory_iterator end_iter; for (fs::d irectory_iterator iter (path); iter!=end_iter; ++iter) { if (Fs::is_regular_file (iter- >status ()) { &nbSp; filenames.push_back (Iter->path (). String ()); } if (Fs::is_directory (Iter->status ()) { get_filenames (iter-> Path (). String (), filenames); } } return filenames.size (); } Description: file_status status (Path);
Returns the state of the path name
Path Iterator Basic_directory_iterator
constructor function:
Explicit Basic_directory_iterator (const path& DP);
Basic_directory_iterator ();
Basic_directory_iterator gets the directory from the constructor parameter, and every time the operator++ is invoked, it finds and obtains the next filename until the end of the directory element. Constructors with no parameters Basic_directory_iterator () always construct an end iterator object, which is the only valid iterator for ending a condition.
Sample code that gets all the file names under the specified directory: Void find_file ( const fs::p ath & dir_path ) { if ( !fs::exists ( dir_path ) ) return; &NBSP;&NBSP;&NBSP;&NBSP;FS::d irectory_iterator end_itr; // default construct generates an end iterator for ( fs::d irectory_iterator itr ( dir_path ); itr != end_itr; ++itr ) { if ( fs:: Is_directory (Itr->status () ) { find_file ( itr->path () ); //Recursive lookup &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} else {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;STD:: Cout << *itr << std::endl; }  }}
boost Recursive Traversal folder
#include <boost/filesystem.hpp> #include <string> void Resucurepath (boost::filesystem::p Ath Src_path,
Boost::filesystem::p ath Dest_path) {using namespace Boost::filesystem;
if (Is_directory (Src_path)) {Directory_iterator tmp_directory_end;
Directory_iterator tmp_dir_it (Src_path);
for (tmp_dir_it; tmp_dir_it!= tmp_directory_end; tmp_dir_it++) {Path Child_dest_path = Dest_path;
if (Is_directory (*tmp_dir_it)) {std::string tmp_dir_name = (*tmp_dir_it). Path (). FileName (). String ();
Child_dest_path.append (Tmp_dir_name.begin (), Tmp_dir_name.end ());
if (!exists (Child_dest_path)) {create_directory (Child_dest_path);
} std::cout << Child_dest_path << Std::endl;
else {std::string tmp_dir_name = (*tmp_dir_it). Path (). Stem (). String ();
Child_dest_path.append (Tmp_dir_name.begin (), Tmp_dir_name.end ()); } Resucurepath (*tmp_dir_iT). Path (), Child_dest_path);
} else if (Is_regular_file (Src_path)) {file *fp (NULL);
fp = fopen (Dest_path.string (). C_STR (), "w+b");
Fclose (FP);
fp = NULL;
Std::cout << Dest_path << Std::endl;
} void Main () {using namespace Boost::filesystem;
Path Src_path ("E:/test/test_dir/test_src");
Path Dest_path ("E:/test/test_dir/test_dest");
Resucurepath (Src_path, Dest_path); }