Skip to content

Instantly share code, notes, and snippets.

@vivithemage
Created March 12, 2014 22:18
Show Gist options
  • 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;
}
}
}
@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