Created
March 12, 2014 22:18
-
-
Save vivithemage/9517678 to your computer and use it in GitHub Desktop.
list all files in directory with boost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "boost/filesystem.hpp" | |
using namespace std; | |
using namespace boost::filesystem; | |
int main(int argc, char *argv[]) | |
{ | |
// list all files in current directory. | |
//You could put any file path in here, e.g. "/home/me/mwah" to list that directory | |
path p ("."); | |
directory_iterator end_itr; | |
// cycle through the directory | |
for (directory_iterator itr(p); itr != end_itr; ++itr) | |
{ | |
// If it's not a directory, list it. If you want to list directories too, just remove this check. | |
if (is_regular_file(itr->path())) { | |
// assign current file name to current_file and echo it out to the console. | |
string current_file = itr->path().string(); | |
cout << current_file << endl; | |
} | |
} | |
} |
boost::filesystem::path targetDir("C:\\dir");
boost::filesystem::recursive_directory_iterator iter(targetDir), eod;
BOOST_FOREACH(boost::filesystem::path const& i, make_pair(iter, eod)){
if (is_regular_file(i)){
cout << i.string() << endl;
}
}
std::vector<std::string>& get_file_list(const std::string& path)
{
if (!path.empty())
{
namespace fs = boost::filesystem;
fs::path apk_path(path);
fs::recursive_directory_iterator end;
for (fs::recursive_directory_iterator i(apk_path); i != end; ++i)
{
const fs::path cp = (*i);
m_file_list.push_back(cp.string());
}
}
return m_file_list;
}
works fine, thanks
works,thx
for(auto & p : boost::filesystem::directory_iterator( path )){ std::cout << p << std::endl; }
Pretty straight-forward, also works with 2017's C++
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, Unfortunately this does not work for me... The program always throws an error at line 11. Apparently the string iterators are incompatible?