仅列出目录中的文件夹 [英] Listing only folders in directory

查看:73
本文介绍了仅列出目录中的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出C ++目录中的文件夹,最好以可移植(在主要操作系统上工作)的方式列出.我尝试使用POSIX,它可以正常工作,但是如何识别找到的项目是否是文件夹?

I want to list folders in a directory in C++, ideally in a portable (working in the major Operating Systems) way. I tried using POSIX, and it works correctly, but how can i identify whether the found item is a folder?

推荐答案

使用C ++ 17 std::filesystem 库:

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;
}

这篇关于仅列出目录中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆