Fast Get File Modification Time

Posted on 1998-05-27
Last Modified: 2013-12-03
Giving the path of a file, does anyone know a faster way to get the LastWrite time, than the following ?

// Try with GetFileTime, fast but I think it may fail if file is locked, but I haven't been able to provoke it

TimeFound = 0;
if (h >= 0) {
  if (GetFileTime(h, CreateTime, LastAccess, LastWrite)) {
   TimeFound = 1;

if (!TimeFound) {
  // Get time using FindFirstFile, slow but don't care if file locked.
Question by:hustch
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 22

Expert Comment

ID: 1404249
That looks like that should work well.  It shouldn't be too slow even when the FindFirstFile needs to be used.  

The "FILE_SHARE_READ or FILE_SHARE_WRITE" should be expressed as "FILE_SHARE_READ || FILE_SHARE_WRITE"  in C or C++, however.
LVL 23

Expert Comment

ID: 1404250
What do you mean by a faster way? One function call or faster speed?

If you are looking for one function call:

On Windows NT 4.0 or later, you can use the GetFileAttributesEx function to get the information.

You can also use FindFirstFile. But you have to use FindClose.

By the way, it should be | instead of ||.
LVL 22

Expert Comment

ID: 1404251
Opps.  Correct a mistake with a mistake.  Yes,


Chensu got me thinking too.  Looking at what you had I was lured into thinking that by opening the file with CreateFile() the OS would not have to search the directory tree and would therefore be faster.  That was stupid on my part.  The OS will still search, only it will do it on the open rather than on the procedure that gets the time.  In addition there is considerable overhead for opening and closing a file.  You are probably best off just using FindFirstFile (and associated procedures) in all cases.
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.


Author Comment

ID: 1404252
The or got there because it is actually Delphi code, that I translated because it is not the Delphi forum.
I know I have to use FindClose.

By faster, I mean shorter execution times.

The CreateFile is a lot faster, I don't know why, but I timed it. There is a lot of files in the directory.
The parameters to CreateFile was chosen in an attempt to avoid sharing problems.
I'll try GetFileAttributesEx
LVL 22

Expert Comment

ID: 1404253
There are probably plenty of people here who "speak" delphi, so you ussually shouldn't have to translate.

When you use FindFirstFile(), do you use a path that specifcally indicates the file?  You don't use wildcards and then look fo the file do you?

Author Comment

ID: 1404254
The GetFileAttirbutesEx is faster than the CreateFile approach.
It is what i wanted when I started, but I can't find using Delphi help, but I found it using MSVC++ help.

chensu, make an answer with GetFileAtttibutesEx, and I will accept it.
LVL 23

Accepted Solution

chensu earned 100 total points
ID: 1404255
On Windows NT 4.0 or later, you can use the GetFileAttributesEx function to get the information.

Thank you.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.Net Loading Word 11 109
Example of WebAPI 7 36
SignTool Error with Windows 10 5 258
Infrastructure development vs software development 3 107
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question