查找第一个文件W通配符匹配

FindFirstFileW wildcard match

本文关键字:通配符 第一个 文件 查找      更新时间:2023-10-16

考虑一个独立示例,其中我使用通配符查询目录中的所有名称:

#include <Windows.h>
#include <fstream>
void add_file(const std::string &path)
{
    std::ofstream  ofs(path,std::ofstream::out);
    ofs.close();
}
void foo(const std::wstring& szDir)
{
    std::cout << "f1 : FindFirstFileWn";
    WIN32_FIND_DATAW ffd;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    hFind = FindFirstFileW(szDir.c_str(), &ffd);
    if (INVALID_HANDLE_VALUE == hFind) 
    {
        std::cout << "Error in FindFirstFileW : " << GetLastError() << std::endl;
        return;
    } 
    // List all the files in the directory with some info about them.
    do
    {
        std::wcout <<"Long file name " << "  " <<  ffd.cFileName << std::endl;
        std::wcout <<"Short file name " << "  " <<  ffd.cAlternateFileName << std::endl;
    }
    while (FindNextFileW(hFind, &ffd) != 0);
    FindClose(hFind);
}
int main()
{
    const char  odd_filename[] = {static_cast<char>(0xC4U), '.', 't', 'x', 't', 0};
    add_file("C:\mydir1\777.Txt");
    add_file(std::string("C:\mydir1\") + std::string(odd_filename));
    foo(L"C:\mydir1\7*");
    return 0;
}

这给了我如下输出

f1 : 查找第一个文件W长文件名 777.Txt短文件名长文件名 ─.txt短文件名 7F7E~1.TXT

为什么FindFirstFileW返回第二个文件名Ä.txt作为匹配项?

通配符匹配适用于长文件名和短文件名。第二个文件的短名称为 7F7E~1.TXT,因此与 7* 匹配。

文档是这样介绍的:

以下列表标识了一些其他搜索特征:

  • 搜索严格基于文件名执行,而不是基于日期或文件类型等任何属性(有关其他选项,请参阅 FindFirstFileEx(。
  • 搜索包括长文件名和短文件名。
  • 尝试使用尾随反斜杠打开搜索总是失败。
  • lpFileName 参数传递无效字符串、NULL 或空字符串不是此函数的有效用法。在这种情况下的结果 未定义。

第二个要点是相关的