Using GetFileTime and CompareFileTime

Could you please explain, along with examples, how to use the commands GetFileTime and CompareFileTime? I read the help topics but find it hard to digest what they say. I want to compare two files’ last modified date. I haven’t really used file in Delphi before but I’ve used it in standard Pascal. Any help would be appreciated.

Aren’t Windows API functions fun? Hehe…

Seriously, there’s a bit of work you have to do, but it’s not that difficult. The first thing you have to do when using Windows API calls is to examine the input parameters carefully?this is where the online help comes in handy. The declaration for GetFileTime is as follows:

BOOL GetFileTime(            HANDLE hFile,                LPFILETIME lpCreationTime,            LPFILETIME lpLastAccessTime,            LPFILETIME lpLastWriteTime );

Yikes! It’s a little confusing. But the Delphi prototype declaration is a bit easier on the eyes:

function GetFileTime(hFile: THandle;  lpCreationTime, lpLastAccessTime, lpLastWriteTime:PFileTime): BOOL; stdcall;

PFileTime is a pointer to a _FileTime structure. Notice that the type is preceded by an underscore. This format is just a naming convention established by the Delphi development team to tell you that it’s something that you probably shouldn’t mess with in the source file. _FileTime has the following structure:

  PFileTime = ^TFileTime;  _FILETIME = record    dwLowDateTime: DWORD;    dwHighDateTime: DWORD;  end;

Did you notice the PFileTime pointer declaration? Moving on, the first parameter of GetFileTime is hFile, which is a THandle type. Okay, so how do we get that? For that, we use CreateFile. In Win32, CreateFile is the preferred method for file access. OpenFile is used for 16-bit compatibility. Look in the online help for a discussion of this call. It’s fairly detailed.

Now we’ve got all the pieces together, except for the details of CompareFileTime. CompareFileTime is pretty easy?you just pass two TFileTime structures to it and it’ll return a LongInt that tells you which file is older than which, if at all. So in that case, I’ve come up with a wrapper function that will handle all the details of initializing variables and doing the calls you need to compare the file times. Mind you, this function is a bit off the top of my head, so you’ll have to play with it to see how it works (read: debug it yourself). It should give you an idea of how to progress. The function is called CompareFileTimes and is declared as follows:

function CompareFileTimes(File1, File2 : String) : LongInt;var  F1, F2          : THandle;  F1_CreateTime,  F1_LastAccess,  F1_LastWrite,  F2_CreateTime,  F2_LastAccess,  F2_LastWrite    : PFileTime;begin  //Initialize all variables  F1 := 0;  F2 := 0;  //Since these are pointers, we have to  //allocate memory for the FileTime structures  GetMem(F1_CreateTime, SizeOf(TFileTime));  GetMem(F1_LastAccess, SizeOf(TFileTime));  GetMem(F1_LastWrite, SizeOf(TFileTime));  GetMem(F2_CreateTime, SizeOf(TFileTime));  GetMem(F2_LastAccess, SizeOf(TFileTime));  GetMem(F2_LastWrite, SizeOf(TFileTime));  //Fill the structures with nulls for now  FillChar(F1_CreateTime, SizeOf(TFileTime), #0);  FillChar(F1_LastAccess, SizeOf(TFileTime), #0);  FillChar(F1_LastWrite, SizeOf(TFileTime), #0);  FillChar(F2_CreateTime, SizeOf(TFileTime), #0);  FillChar(F2_LastAccess, SizeOf(TFileTime), #0);  FillChar(F2_LastWrite, SizeOf(TFileTime), #0);  //Get file handles for the files in question  //Notice that even though we're using CreateFile  //the open disposition for the file is OPEN_EXISTING  F1 := CreateFile(PChar(F1), 0, FILE_SHARE_READ,                   nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);  F2 := CreateFile(PChar(F2), 0, FILE_SHARE_READ,                   nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);  //Get the file times for the files.  GetFileTime(F1, F1_CreateTime, F1_LastAccess, F1_LastWrite);  GetFileTime(F2, F2_CreateTime, F2_LastAccess, F2_LastWrite);  //Assign the function's result to comparison  //-1, File1 is younger than File2  //0, File1 is the same as File2  //+1 File1 is older than File2  Result := CompareFileTime(F1_CreateTime^, F2_CreateTime^);  //Free the memory allocated to the pointers  FreeMem(F1_CreateTime, SizeOf(TFileTime));  FreeMem(F1_LastAccess, SizeOf(TFileTime));  FreeMem(F1_LastWrite, SizeOf(TFileTime));  FreeMem(F2_CreateTime, SizeOf(TFileTime));  FreeMem(F2_LastAccess, SizeOf(TFileTime));  FreeMem(F2_LastWrite, SizeOf(TFileTime));end;

That’s about it in a nutshell. Have at it!

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a