Find All Files in Directory and Subdirectories using C++

std::filesystem::recursive_directory_iterator function

#include <iostream>
#include <filesystem>

int main()
{
    std::string rootDir = "test_dir";

    std::vector<std::string> files;

    for (std::filesystem::recursive_directory_iterator it(rootDir), end; it != end; ++it) {
        if (!is_directory(it->path())) {
            files.push_back(it->path().string());
        }
    }

    for (std::string const& filePath : files) {
        std::cout << filePath << std::endl;
    }

    return 0;
}

Leave a Comment

Cancel reply

Your email address will not be published.