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;
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With