开发者

traversing a directory

开发者 https://www.devze.com 2022-12-25 08:46 出处:网络
Is there any way to tr开发者_高级运维averse the content of a directory? I would like to store the names of each folder in a given directory.

Is there any way to tr开发者_高级运维averse the content of a directory? I would like to store the names of each folder in a given directory.

Thanks!


According to the tags you're interested in C++/Boost. Then, kindly borrowing from this SO answer:

#include <utility>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>

#define foreach BOOST_FOREACH
namespace fs = boost::filesystem;

fs::recursive_directory_iterator it(top), eod;
foreach (fs::path const & p, std::make_pair(it, eod)) {
    if (is_directory(p)) {
        ...
    } else if (is_regular_file(p)) {
        ...
    } else if (is_symlink(p)) {
        ...
    }
}

Another version, taken from Rosetta code:

#include "boost/filesystem.hpp"
#include "boost/regex.hpp"
#include <iostream>

using namespace boost::filesystem;

int main()
{
  path current_dir("."); //
  boost::regex pattern("a.*"); // list all files starting with a
  for (recursive_directory_iterator iter(current_dir), end;
       iter != end;
       ++iter)
  {
    std::string name = iter->path().leaf();
    if (regex_match(name, pattern))
      std::cout << iter->path() << "\n";
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号