FindFirstFileW通配符匹配

Rec*_*ker 3 c++ windows winapi

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

#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 : FindFirstFileW\n";
    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;
}
Run Code Online (Sandbox Code Playgroud)

这给我输出如下

f1:FindFirstFileW
长文件名777.Txt
短文件名
长文件名?.txt
短文件名7F7E〜1.TXT

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

Dav*_*nan 5

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

文件涵盖了这个像这样:

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

  • 搜索仅在文件名上执行,而不在日期或文件类型之类的任何属性上执行(有关其他选项,请参见 FindFirstFileEx)。
  • 搜索包括长文件名和短文件名。
  • 尝试打开带有反斜杠的搜索始终会失败。
  • lpFileName参数传递无效的字符串NULL或空字符串不是此函数的有效用法。在这种情况下,结果是不确定的。

第二个要点是相关的。

  • 除了浪费目录中的空间和时间以及获取目录列表之外,此问题是我始终禁用NTFS中旧式短名称的另一个原因。ReFS甚至不支持它们。依赖于它们的存在的代码已过时。 (3认同)