Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:47:20+00:00 2026-06-16T02:47:20+00:00

On my server there are a few files with Modified Date 31/DEC/1979 (Don’t ask

  • 0

On my server there are a few files with Modified Date 31/DEC/1979 (Don’t ask me why). So FileExists returns false.

Sysutils.FileExists looks like this:

function FileAge(const FileName: string): Integer;
var
  Handle: THandle;
  FindData: TWin32FindData;
  LocalFileTime: TFileTime;
begin
  Handle := FindFirstFile(PChar(FileName), FindData);
  if Handle <> INVALID_HANDLE_VALUE then
  begin
    Windows.FindClose(Handle);
    if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
    begin
      FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
      if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,
        LongRec(Result).Lo) then Exit;
    end;
  end;
  Result := -1;
end;

function FileExists(const FileName: string): Boolean;
begin
  Result := FileAge(FileName) <> -1;
end;

My question is, Why does the function depends on FileAge in the first place?
Isn’t the following line sufficient?:

if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
  // Yes the file exists!

Or even based on file attributes:

function MyFileExists(const Name: string): Boolean;
var
  R: DWORD;
begin
  R := GetFileAttributes(PChar(Name));
  Result := (R <> DWORD(-1)) and ((R and FILE_ATTRIBUTE_DIRECTORY) = 0);
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:47:21+00:00Added an answer on June 16, 2026 at 2:47 am

    The modern versions of Delphi implement FileExists in broadly the same way as your code does. The implementation has extra handling for symlinks but is otherwise essentially identical to your version.

    There’s one interesting nuance in the modern Delphi implementation. If the call to GetFileAttributes returns INVALID_FILE_ATTRIBUTES, then the code doesn’t immediately bail out. Instead it does this:

    LastError := GetLastError;
    Result := (LastError <> ERROR_FILE_NOT_FOUND) and
      (LastError <> ERROR_PATH_NOT_FOUND) and
      (LastError <> ERROR_INVALID_NAME) and ExistsLockedOrShared(Filename);
    

    And the implementation of ExistsLockedOrShared uses FindFirstFile and a check of the FILE_ATTRIBUTE_DIRECTORY on dwFileAttributes. This indicates that GetFileAttributes can fail when the file exists, but is locked. But that FindFirstFile can succeed in such a scenario. That’s reasonable because FindFirstFile uses the file metadata rather than the data stored in the file itself.

    It’s hard to say why the code is is the way it is in the older versions. I think it’s weak. Personally I would replace FileExists with a better version, using a code hook. For example: Patch routine call in delphi

    As always, there’s a Raymond Chen article on the subject: Superstition: Why is GetFileAttributes the way old-timers test file existence?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are quite a few of IRC server codes I am working on a
We have a few tables with persisted computed columns in SQL Server. Is there
currently we have a TFS Server that has a few files with many branches,
In SQL Server there is a way to join tables from multiple sql servers
On a server there are several independent Plone buildout installations, running under different UNIX
In sql-server there is function to auto-increment guid fields. CREATEGUID() how can I do
In connection string to SQL Server there is max pool size option. My question
In my project ,I get json data from Server,there's a field named 'creat_at' in
I got a asp site hosted in a iis server. In the server there
I've been trying to do the following with mod_rewrite on the server there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.