Skip to content

Instantly share code, notes, and snippets.

@vivithemage
Created March 12, 2014 22:18
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vivithemage/9517678 to your computer and use it in GitHub Desktop.
Save vivithemage/9517678 to your computer and use it in GitHub Desktop.
list all files in directory with boost
#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;
}
}
}
@kevin1michael
Copy link

Hello, Unfortunately this does not work for me... The program always throws an error at line 11. Apparently the string iterators are incompatible?

@belalmoh
Copy link

    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;
    }
}

@osolovyoff
Copy link

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;
}

@m-turambar
Copy link

works fine, thanks

@linzha0
Copy link

linzha0 commented Aug 8, 2016

works,thx

@DaniBarca
Copy link

DaniBarca commented Jan 9, 2018

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