BUG: FindFirstFile() Does Not Handle Wildcard (?) Correctly

Last reviewed: September 25, 1995
Article ID: Q130860
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows 95 version 4.0
    

SYMPTOMS

In Windows 95, the FindFirstFile() function interprets a wildcard (?) as "any character" instead of "zero or one character," its true meaning. This incorrect interpretation causes some searches to return invalid results. For example, if the files, TEMP.TXT and TEMPTEMP.TXT, are in the same directory, the following code finds the TEMPTEMP.TXT file, but not the TEMP.TXT file:

   HANDLE hFind;
   WIN32_FIND_DATA findData = {0};

   hFind = FindFirstFile ("TEM?????.???", &findData);

   if (hFind == INVALID_HANDLE_VALUE)
      MessageBox (hwnd, "FindFirstFile() failed.", NULL, MB_OK);
   else
   {
      do
      {
         MessageBox (hwnd, findData.cFileName, "File found", MB_OK);
      }
      while (FindNextFile(hFind, &findData));

      CloseHandle (hFind);
   }

Windows NT correctly finds both the TEMP.TXT and TEMPTEMP.TXT files.

RESOLUTION

To work around this problem, choose an alternative wildcard search and apply further processing to eliminate files that are found by the alternative search, but do not match the original search. For example, the code above could be changed to search for TEM*.* instead of TEM?????.???. Then you could make an additional test for filenames that are up to 8 characters in length, followed by a ".", followed by up to 3 more characters (8.3).

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.


Additional reference words: 95 4.00 regular expression wild
KBCategory: kbprg kbbuglist
KBSubcategory: BseFileio


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 25, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.