How to know and load all images in a specific folder?

c++, c++builder, windows-xp

Solution

I did something like this yesterday with C++ using the boost::filesystem library. However, if you are not using boost already, I would strongly recommend you just use the windows libraries instead. This was my code though in case you're interested:

#include <algorithm>
#include <boost/filesystem.hpp>
#include <set>

namespace fs = boost::filesystem;

typedef std::vector<fs::path> PathVector;

std::auto_ptr<PathVector> ImagesInFolder(const fs::path& folderPath) {
    std::set<std::string> targetExtensions;
    targetExtensions.insert(".JPG");
    targetExtensions.insert(".BMP");
    targetExtensions.insert(".GIF");
    targetExtensions.insert(".PNG");

    std::auto_ptr<PathVector> paths(new PathVector());

    fs::directory_iterator end;
    for(fs::directory_iterator iter(folderPath); iter != end; ++iter) {
        if(!fs::is_regular_file(iter->status())) { continue; }

        std::string extension = iter->path().extension();
        std::transform(extension.begin(), extension.end(), extension.begin(), ::toupper);
        if(targetExtensions.find(extension) == targetExtensions.end()) { continue; }

        paths->push_back(iter->path());
    }

    return paths;
}

This doesn't answer the part of your question about how to actually put the paths into a listbox though.

Problem

I have an application (C++ Builder 6.0) that needs to know the total of images there are in a specific folder, and then I have to load them: in an ImageList or in a ComboBoxEx... or any other control... How can I do that? I know how to load an image in a control, or to save in a TList, or in an ImageList... but How to know how many files files there are in the directory, and how to load every image in it?? I am Sorry about my English.

Original source