使用纯c ++(跨平台)获取特定路径中的所有目录/文件和子目录

Beyondo

如何使用现代C ++获取特定路径中目录/文件和子目录的向量?

Beyondo

std::filesystem..!中使用递归函数

#if __cplusplus < 201703L// If the version of C++ is less than 17
// It was still in the experimental:: namespace
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
std::vector<fs::path> geteverything(const fs::path &path)
{
    std::vector<fs::path> dirs;
    for (const auto & entry : fs::directory_iterator(path))
    {
        dirs.push_back(entry);
        if (fs::is_directory(entry))
        {
            auto subdirs = geteverything(entry);
            dirs.insert(dirs.end(), subdirs.begin(), subdirs.end());
        }
    }
    return dirs;
}

测试

// Change this to the absolute/relative path you'd like to fetch.
std::string path = "C:/Windows/Temp";
int main()
{
    std::cout << "fetching all directories and files in : " << path << " ...\n";
    auto list = geteverything(path);
    for (const auto &path : list)
        std::cout << path << "\n";
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试使用Bat文件获取目录和子目录中所有文件的行数

使用子目录列出指定路径中具有完整路径和大小的所有文件名

如何使用vim打开当前目录和所有子目录中的所有文件?

使用路径获取Java中的特定子目录

使用Java以非递归方式获取目录及其子目录中的所有文件

列出目录和子目录中的所有文件C#

使用bash terminal命令打开目录和子目录中的所有文件?

如何使用通过FTP的骆驼路由将所有文件从目录(包括子目录)移动到特定目录,而目标中没有子目录?

如何使用MediaStore获取特定目录和所有子目录中所有音乐的列表

使用git查看子目录中与特定文件扩展名相关的所有日志

PHP:使用 scandir 从多个子目录中获取所有 zip 文件

为特定目录中除子目录外的所有C ++文件生成一个标记文件

使用 Powershell 重命名当前目录中包含文件和子目录的所有文件夹名称

如何使用#ifdef和#endif注释掉目录和子目录中所有文件中的垂直线?

列出子目录及其父目录中的所有c文件

使用Unix遍历目录及其子目录中的所有文件

使用终端将内容/文件复制到目录中的所有子目录

遍历 C++ 中的所有目录和子目录

使用多种条件优化find -exec {}:目录中的特定文件和该目录中的特定子目录

在 perl 的所有子目录中使用特定字符串的特定文件的 grep

交互式查找和替换所有文件,包括使用 Vim 的子目录中的文件

如何使用Java的目录流仅在目录中而不是其他子目录中获取文件/子目录

Vim:在所有子目录中打开所有.c和.h

使用find排除所有子目录的文件

如何使用Grunt压缩子文件夹/子目录中的所有JavaScript文件?

C# 获取特定的子目录

使用.gitignore取消忽略子目录中的特定文件

如何在不使用find的情况下在当前目录的直接子目录中获取名称中包含某些模式的所有文件?

C ++中的递归列表文件未输入所有子目录