Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

FindFirstFile matches wildcards that shouldn't match

On some of my Windows 10 machines, FindFirstFile matches files that definitely should not match. Assume the following program in Delphi:

{$apptype console}
uses Windows;

var
  FindHandle: THandle;
  FindData: WIN32_FIND_DATA;

begin
  FindHandle := FindFirstFile('*.qqq', FindData);
  if FindHandle <> INVALID_HANDLE_VALUE then
  begin
    try
      repeat
        Writeln(PChar(@FindData.cFileName[0]));
      until not FindNextFile(FindHandle, FindData);
    finally
      FindClose(FindHandle);
    end;
  end;
end.

and four files:

a.qqq
b.qqqt
c.qqqx
c.qqq123

The output I expect to get is just a.qqq. But what actually happens is, all four files get printed out. I get the same result with CMD’s dir *.qqq, too, so it’s not just Delphi doing weird stuff, but PowerShell’s dir *.qqq works as expected. What could possibly be causing this behavior? And particularly, if it is some specific settings in the OS (which seems to be indicated by the fact that I don’t get this result on all machines, just some), is there something I can do from within my program to enforce the expected behavior regardless of the OS settings?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The reason is that the underlying Windows functions checks the long and the short file names:

The search includes the long and short file names.

You can see that when you add the /X parameter to the dir call in CMD:

dir /X *.qqq

A possible solution is to add another filter check for each found name that only takes the long name.

Actually, that is what Delphi does in TDirectory.GetFiles, making that sometimes a better alternative to the hand written routine.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading