Using GetFileTime and CompareFileTime

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.

See also  11 Effective Keyword Research Tools and Techniques

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!

See also  Does It Make Sense to Splurge on a Laptop?

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist