Solved

Fast Get File Modification Time

Posted on 1998-05-27
7
396 Views
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;
h = CreateFile(FileName, 0, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 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.
}
0
Comment
Question by:hustch
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:nietod
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.
0
 
LVL 23

Expert Comment

by:chensu
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 ||.
0
 
LVL 22

Expert Comment

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

"FILE_SHARE_READ | FILE_SHARE_WRITE"

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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:hustch
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
0
 
LVL 22

Expert Comment

by:nietod
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?
0
 
LVL 1

Author Comment

by:hustch
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.
0
 
LVL 23

Accepted Solution

by:
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now