问题
I'm developing a C++ console application that has to manage files on Windows OS. I need to obtain a 'list' of file names which have a specific extension. I've found a lot of solutions, the most suggested one is the following one:
HANDLE hFind;
WIN32_FIND_DATA data;
hFind = FindFirstFile("C:\\PWS\\*.sda", &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
cout << data.cFileName << endl;
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
Suppose I have these files inside the C:\\PWS folder:
- f001.sdac
- f002.sda
- f003.sdab
- f004.sda
The above code prints all of them, while I only need f002.sda and f004.sda.
Any hint?
NB: I don't want to use boost library.
回答1:
The code also finds "f001.sdac" and "f003.sdab" because FindFirstFile() and FindFirstFileEx() match both the short (8.3) and long file names. For instance, the short file name for "f001.sdac" may be something like "f~1.sda".
The reason for that is backwards compatibility with 16-bit (sigh!) programs.
To work around this issue, call FindFirstFile() with the * wildcard to match all files and then do your own filtering, for instance by calling the PathMatchSpec() function. PathMatchSpec() does string matching only so it doesn't have the weird behaviour of FindFirstFile().
WIN32_FIND_DATAW data;
HANDLE hFind = FindFirstFileW( L"C:\\PWS\\*", &data );
if( hFind != INVALID_HANDLE_VALUE )
{
do
{
if( ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 &&
PathMatchSpecW( data.cFileName, L"*.sda" ) )
{
std::wcout << data.cFileName << std::endl;
}
}
while( FindNextFileW( hFind, &data ) );
FindClose( hFind );
}
Side note: Calling FindFirstFileEx() with the value of FindExInfoBasic for the fInfoLevelId parameter, which supposedly "does not query the short file name", is not a valid solution for this issue because it will still match short (8.3) file names.
回答2:
Something like this should filter the results for you:
bool hasFileExtension(TCHAR cFileName[], TCHAR* ptcExtension)
{
bool result = true;
int iFileNameLength = _tcslen(cFileName);
int iExtensionLength = _tcslen(ptcExtension);
if (iFileNameLength >= iExtensionLength)
{
for (int i = 1; i < iExtensionLength + 1 && result; i++)
{
if (cFileName[iFileNameLength - i] != ptcExtension[iExtensionLength - i])
{
result = false;
}
}
}
else
{
result = false;
}
return result;
}
void listFilesWithExtension(LPCTSTR lpFileName, TCHAR* ptcExtension)
{
HANDLE hFind;
WIN32_FIND_DATA data;
hFind = FindFirstFile(lpFileName, &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (hasFileExtension(data.cFileName, ptcExtension))
{
wcout << data.cFileName << endl;
}
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
}
int main()
{
LPCTSTR lpFileName = L"C:\\PWS\\*.sda";
TCHAR* ptcExtension = _T(".sda");
listFilesWithExtension(lpFileName, ptcExtension);
Sleep(5000);
return 0;
}
回答3:
The solution I adopted is the following one:
void GetFilesByNameRootAndExtension(const string& dirPath, const string& nameRoot, string& ext, vector<string>& files)
{
files.clear();
stringstream ss;
ss << dirPath << "\\" << nameRoot << "*" << ext;
string searchKeyS = ss.str();
const char* searchKey = searchKeyS.c_str();
WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile(searchKey, &data);
ext = ext.erase(0, 1);
if (hFind != INVALID_HANDLE_VALUE) {
do {
string fN = data.cFileName;
if (fN.substr(fN.find_last_of(".") + 1) == ext) // filtering by extension
files.push_back(fN);
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
}
Thank you everybody for your hints!
来源:https://stackoverflow.com/questions/44920957/searching-files-filtering-by-extension-returns-too-many-results