| 1452 | |
| 1453 | |
| 1454 | bool DoesFilePatternExist(LPTSTR aFilePattern, DWORD *aFileAttr, DWORD aRequiredAttr) |
| 1455 | // Returns true if the file/folder exists or false otherwise. |
| 1456 | // If non-NULL, aFileAttr's DWORD is set to the attributes of the file/folder if a match is found. |
| 1457 | // If there is no match, its contents are undefined. |
| 1458 | { |
| 1459 | if (!aFilePattern || !*aFilePattern) return false; |
| 1460 | // Fix for v1.0.35.12: Don't consider the question mark in "\\?\Volume{GUID}\" to be a wildcard. |
| 1461 | // Such volume names are obtained from GetVolumeNameForVolumeMountPoint() and perhaps other functions. |
| 1462 | // However, testing shows that wildcards beyond that first one should be seen as real wildcards |
| 1463 | // because the wildcard match-method below does work for such volume names. |
| 1464 | LPTSTR cp = _tcsncmp(aFilePattern, _T("\\\\?\\"), 4) ? aFilePattern : aFilePattern + 4; |
| 1465 | if (StrChrAny(cp, _T("?*"))) |
| 1466 | { |
| 1467 | WIN32_FIND_DATA wfd; |
| 1468 | HANDLE hFile = FindFirstFile(aFilePattern, &wfd); |
| 1469 | if (hFile == INVALID_HANDLE_VALUE) |
| 1470 | return false; |
| 1471 | // Skip . and .., which appear to always be listed first (for dir\* and dir\*.*). |
| 1472 | while (wfd.cFileName[0] == '.' && (!wfd.cFileName[1] || wfd.cFileName[1] == '.' && !wfd.cFileName[2])) |
| 1473 | if (!FindNextFile(hFile, &wfd)) |
| 1474 | return false; |
| 1475 | if (aRequiredAttr) // Caller wants to check for a file/folder with specific attributes. |
| 1476 | { |
| 1477 | while ((wfd.dwFileAttributes & aRequiredAttr) != aRequiredAttr) |
| 1478 | if (!FindNextFile(hFile, &wfd)) |
| 1479 | return false; |
| 1480 | // Since above didn't return, a file/folder with the required attribute was found. |
| 1481 | } |
| 1482 | FindClose(hFile); |
| 1483 | if (aFileAttr) |
| 1484 | *aFileAttr = wfd.dwFileAttributes; |
| 1485 | return true; |
| 1486 | } |
| 1487 | else |
| 1488 | { |
| 1489 | DWORD attr = GetFileAttributes(aFilePattern); |
| 1490 | if (aRequiredAttr && (attr & aRequiredAttr) != aRequiredAttr) |
| 1491 | return false; |
| 1492 | if (aFileAttr) |
| 1493 | *aFileAttr = attr; |
| 1494 | return attr != 0xFFFFFFFF; |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | |
| 1499 |
no test coverage detected