Рассмотрим отдельный пример, в котором я запрашиваю все имена в каталоге с подстановочным знаком:
#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*.
документация охватывает это следующим образом:
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.
Второй пункт списка относится к делу.
Рэймонд Чен также рассказал об этом в своем блоге: Почему FindFirstFile находит короткие имена?
действительно никогда не нужно использовать FindFirstFile, но как минимум FindFirstFileEx с FindExInfoBasic
Помимо потраченного впустую места и времени в каталогах и при получении списка каталогов, эта проблема является еще одной причиной, по которой я всегда отключаю устаревшие короткие имена в NTFS. ReFS их даже не поддерживает. Код, основанный на их существовании, устарел.