Delphi: what's wrong with FileExists
2007-07-27 08:33 i1250 [permalink]
When you write a Delphi application (Win32, I guess this is not an issue when you use .Net) that handles a lot of files that come from devices and storage media, you may have come across a little problem with FileExists.
When the file's last modified date is set to an invalid date, or got corrupted, you'll notice it won't show in the list when viewing the directory in Windows. The file itself exists and may be perfectly valid. Still Delphi's FileExists may result false on the file.
FileExists internally uses FileAge to check whether the file is there. FileAge gives an integer value that reflects the last modified date of a file, which is really convenient for checking if a file got changed by last time you checked, but also fails (returns -1) on files that have the last modified date missing or corrupt.
One way to circomvent is to define a new FileExists function that directly checks with the file-system of the file exists.
function FileExists2(const FileName: string): boolean;
Handle := FindFirstFile(PChar(FileName), FindData);
if Handle <> INVALID_HANDLE_VALUE then
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY)=0 then