Skip to content

Instantly share code, notes, and snippets.

@yiling-chen
Last active December 21, 2015 06:38
Show Gist options
  • Save yiling-chen/6265303 to your computer and use it in GitHub Desktop.
Save yiling-chen/6265303 to your computer and use it in GitHub Desktop.
List all image files (jpeg) in a specific folder.
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
using namespace boost::filesystem;
using std::endl;
using std::cout;
using std::cerr;
int main(int argc, char* argv[]) {
path p(argv[1]);
try {
if (exists(p)) {
if (!is_directory(p)) {
cerr << "Please specify a valid directory." << endl;
return 1;
}
else {
cout << p << " is a directory containing:\n\n";
directory_iterator end_iter; // the default constructor creates the "end" iterator
for (directory_iterator dir_iter(p); dir_iter != end_iter; ++dir_iter) {
if (is_regular_file(dir_iter->status())) {
String filename = boost::to_lower_copy(dir_iter->path().string());
if (filename.find("jpg") != String::npos) {
cout << dir_iter->path().filename() << endl;
cout << filename << endl;
}
}
}
}
}
else {
cerr << "Directory " << p << " does not exist!\n";
return 1;
}
} catch (const filesystem_error &ex) {
cerr << ex.what() << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment