c++ - FindFirstFileW 通配符匹配

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

这给了我如下输出

f1 : FindFirstFileW
Long file name   777.Txt
Short file name
Long file name   ─.txt
Short file name   7F7E~1.TXT

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

最佳答案

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

documentation像这样涵盖:

The following list identifies some other search characteristics:

  • The search is performed strictly on the name of the file, not on any attributes such as a date or a file type (for other options, see FindFirstFileEx).
  • The search includes the long and short file names.
  • An attempt to open a search with a trailing backslash always fails.
  • Passing an invalid string, NULL, or empty string for the lpFileName parameter is not a valid use of this function. Results in this case are undefined.

第二个要点是相关的。

关于c++ - FindFirstFileW 通配符匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55652472/

相关文章:

java - 仅在为Android编译时才编译源代码?

c# - 以编程方式对开始菜单进行排序

c++ - 在 ListView 中禁用水平滚动条

我可以用 C 语言编写的程序编辑 gpedit.msc 吗?

c++ - 查找数组中最大和最小的数字

c++ - 静态成员和 boost 序列化

C++:组合接口(interface)

c++ - 如何在我的成员函数中包含默认参数?

windows - 批处理文件 : How to capture output of a function in a variable?

c++ - 未签名的 WINAPI 函数的正确函数指针是什么?