Get an ordered list of files in a folder
I used boost::filesystem::directory_iterator
to get a list of all available files in a given folder.
The problem is that I assumed that this method would give me the files in alphabetical order, while the results seem to be pretty random.
Is there any fancy way to sort them alphabetically?
My current code:
if(boost::filesystem::is_directory(myFolder)){
// Iterate existing files
boost::filesystem::directory_iterator end_iter;
for(boost::filesystem::directory_iterator dir_itr(myFolder);
dir_itr!=end_iter; dir_itr++){
boost::filesystem::path filePath;
// Check if it is a file
if(boost::filesystem::is_regular_file(dir_itr->status())){
std::cout << "Reading file " << dir_itr->path().string() << std::cout;
}
}
}
source to share
The most convenient way I've seen to accomplish what you want right from the boost
filesystem tutorial . In this particular example, the author appends the file / directory name to the vector and then uses it std::sort
to ensure the data is in alphabetical order. Your code can easily be updated to use the same type of algorithm.
source to share