Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does FindFirst return file names that don't match the mask?

I pass the parameter value '*1.dat' to FindFirst, still the first file that the FindFirst() routine return is 46checks5.dat, very consistently.

Is this a known problem?

vpath:=trim(vpath);
result:=true;
try
  res:=findfirst(vpath+'\'+vmask,faarchive,search);    //vmask = *1.dat
  try 
    while res=0 do
    begin
      vlist.add(search.name);   //searchname returned is 46checks5.dat!!!
      res:=findnext(search);
    end;
  finally
    findclose(search);
  end;
except
  result:=false;
end;
like image 459
Blow ThemUp Avatar asked Apr 15 '11 16:04

Blow ThemUp


People also ask

What happens if the findfirstfile function fails?

If the function fails because no matching files can be found, the GetLastError function returns ERROR_FILE_NOT_FOUND. The FindFirstFile function opens a search handle and returns information about the first file that the file system finds with a name that matches the specified pattern.

How do I use findfirstfilea?

FindFirstFileA function. Searches a directory for a file or subdirectory with a name that matches a specific name (or partial name if wildcards are used). To specify additional attributes to use in a search, use the FindFirstFileEx function. To perform this operation as a transacted operation, use the FindFirstFileTransacted function.

What is the return type of stream findfirst ()?

Stream findFirst () returns an Optional (a container object which may or may not contain a non-null value) describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.

What is the encoding-neutral alias for findfirstfile?

The fileapi.h header defines FindFirstFile as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors.


1 Answers

The reason is that the file has a "long" name, i.e. with more than 8 characters. For such files Windows also creates "short" names, that usually are created in the form longna~1.dat and this short name is found via *1.dat wildcard.

You can easily reproduce the same behaviour in command prompt in an empty directory:

C:\TEMP>echo. > 46checks5.dat 
C:\TEMP>dir /x *1.dat
 Volume in drive C has no label.
 Volume Serial Number is 5C09-D9DE

 Directory of C:\TEMP

2011.04.15  21:37                 3 46CHEC~1.DAT 46checks5.dat
               1 File(s)              3 bytes

The documentation for FindFirstFile(), which is the underlying API for FindFirst states:

The search includes the long and short file names.

To workaround this issue, then, rather than using Delphi's wrapper to FindFirstFile(), call the Win32 API FindFirstFileEx(). Pass FindExInfoBasic to the fInfoLevelId parameter.

like image 130
andrius Avatar answered Sep 23 '22 22:09

andrius