c++ - Windows 上带通配符的目录中的文件

标签 c++ winapi

如何从包含通配符的路径中轻松获取所有文件路径?例如:C:/Data*Set/Files*/*.txt 我在 Linux 上使用 glob 函数编写了它,但我不能在 Windows 上这样做:/

FindFirstFile 遗憾的是不支持目录名称中的通配符。

我认为应该有可用的 Windows 解决方案,但我找不到。

最佳答案

所以你应该放弃使用操作系统特定的文件访问,支持独立于操作系统:Filesystem Library

假设您得到了 filesystem::path input,其中包含带通配符的路径。要使用它来解决您的问题,您需要:

  1. 使用parent_path输入分解成目录
  2. 使用filename获取输入文件名
  3. 获得 directory_iteratorinput 开始的相对或绝对路径
  4. 创建一个递归函数,该函数将 beginend 迭代器接收到获得的父路径、目录迭代器和文件名
  5. 任何时候目录或文件名使用'*'时,使用带有迭代器的regex来确定下一个应该前进的目录
  6. 要么返回匹配文件的路径,要么返回一个空的path

由于the excellent Ben Voigt's comment我已经更新了算法以跨过非通配目录。

例如:

regex GenerateRegex(string& arg) {
    for (auto i = arg.find('*'); i != string::npos; i = arg.find('*', i + 2)) {
        arg.insert(i, 1, '.');
    }

    return regex(arg);
}

filesystem::path FindFirstFile(filesystem::path directory, filesystem::path::const_iterator& start, const filesystem::path::const_iterator& finish, string& filename) {
    while (start != finish && start->string().find('*') == string::npos) {
        directory /= *start++;
    }
    filesystem::directory_iterator it(directory);
    filesystem::path result;

    if (it != filesystem::directory_iterator()) {
        if (start == finish) {
            for (auto i = filename.find('.'); i != string::npos; i = filename.find('.', i + 2)) {
                filename.insert(i, 1, '\\');
            }
            const auto re = GenerateRegex(filename);

            do {
                if (!filesystem::is_directory(it->status()) && regex_match(it->path().string(), re)) {
                    result = *it;
                    break;
                }
            } while (++it != filesystem::directory_iterator());
        }
        else {
            const auto re = GenerateRegex(start->string());

            do {
                if (it->is_directory() && regex_match(prev(it->path().end())->string(), re)) {
                    result = FindFirstFile(it->path(), next(start), finish, filename);

                    if (!result.empty()) {
                        break;
                    }
                }
            } while (++it != filesystem::directory_iterator());
        }
    }
    return result;
}

可以通过以下方式调用:

const filesystem::path input("C:/Test/Data*Set/Files*/*.txt");

if (input.is_absolute()) {
    const auto relative_parent = input.parent_path().relative_path();

    cout << FindFirstFile(input.root_path(), begin(relative_parent), end(relative_parent), input.filename().string()) << endl;
} else {
    const auto parent = input.parent_path();

    cout << FindFirstFile(filesystem::current_path(), begin(parent), end(parent), input.filename().string()) << endl;
}

Live Example

关于c++ - Windows 上带通配符的目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52111837/

相关文章:

c++ - 重绘屏幕并删除已经存在的内容

c++ - Boost.MPI recv 到现有 vector 的切片中

c++ - 包含 C++ 文件时 Visual Studio 2013 崩溃

基于 C++ 策略的设计

delphi - 如何确定窗口句柄何时有效?

c++ - U 盘上的 "Access is Denied"CreateFile()

C++函数对象术语仿函数、deltor、比较器等

winapi - 关于 ReadFile() WinAPI,GetLastError 抛出错误 183。在这种情况下, "ERROR_ALREADY_EXISTS"是什么意思?

c++ - 有没有办法找到一个dll暴露的所有功能

c++ - 找不到我的代码 :( 有什么问题