Listing only folders in directory
You could use opendir()
and readdir()
to list directories and subdirectories. The following example prints all subdirectories inside the current path:
#include <dirent.h>
#include <stdio.h>
int main()
{
const char* PATH = ".";
DIR *dir = opendir(PATH);
struct dirent *entry = readdir(dir);
while (entry != NULL)
{
if (entry->d_type == DT_DIR)
printf("%s\n", entry->d_name);
entry = readdir(dir);
}
closedir(dir);
return 0;
}
Using the C++17 std::filesystem
library:
std::vector<std::string> get_directories(const std::string& s)
{
std::vector<std::string> r;
for(auto& p : std::filesystem::recursive_directory_iterator(s))
if (p.is_directory())
r.push_back(p.path().string());
return r;
}