1

I need to compare a file's last modified time to a date time stored in a database. I initially looked at this question to get started.

I am currently getting the FILETIME for the last write of the file, converting it to a SYSTEMTIME. Then I use that SYSTEMTIME to create a TDateTime object that I can use for my comparison. However, the FileModifiedDT variable, is always coming out to be the current time, despite the file having been modified previously.

FILETIME lastWriteTime;

String * FileNamePtr = new String( FileName );

GetFileTime( FileNamePtr, NULL, NULL, &lastWriteTime );

SYSTEMTIME systemTime;
FileTimeToSystemTime( &lastWriteTime, &systemTime );

TDateTime * FileModifiedDT = new TDateTime( systemTime.wYear, systemTime.wMonth,
                                            systemTime.wDay, systemTime.wHour,
                                            systemTime.wMinute, systemTime.wSecond,
                                            systemTime.wMilliseconds );

Am I missusing GetFileTime in some way? Is there a better way I should go about this?

Community
  • 1
  • 1
James Hogle
  • 3,010
  • 3
  • 22
  • 46
  • which windows system are you on? – UmNyobe Jul 23 '15 at 14:56
  • windows 8.1 Pro 64bit – James Hogle Jul 23 '15 at 14:57
  • 1
    is `GetFileTime` successful? try with `if(....)` – UmNyobe Jul 23 '15 at 15:02
  • Looks like `GetFileTime` is NOT successful. How can I figure out what error I am getting with it? – James Hogle Jul 23 '15 at 15:05
  • 1
    [GetFileTime](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724320.aspx): *"If the function fails, the return value is zero. To get extended error information, call [**GetLastError**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360.aspx)."* – IInspectable Jul 23 '15 at 15:07
  • follow instructions here : http://stackoverflow.com/questions/455434/how-should-i-use-formatmessage-properly-in-c/455533#455533 – UmNyobe Jul 23 '15 at 15:09
  • I am getting back the error code '6' which seems to mean invalid handle. Would that be referring to my `FileNamePtr` ? – James Hogle Jul 23 '15 at 15:11
  • 1
    Yes. You'll need to pass a `HANDLE` obtained from a call to `CreateFile`, not a path name. – IInspectable Jul 23 '15 at 15:16
  • 1
    On a side note, stop using pointers for types that don't need it: `String sFileName = FileName; ... TDateTime FileModifiedDT( systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds );`. In the case of converting a `SYSTEMTIME` to a `TDateTime`, the RTL has a `SystemTimeToDateTime()` function: `TDateTime FileModifiedDT = SystemTimeToDateTime( systemTime );` – Remy Lebeau Jul 23 '15 at 21:51

1 Answers1

6

The error is

String * FileNamePtr = new String( FileName );
GetFileTime( FileNamePtr, NULL, NULL, &lastWriteTime );

According to the documentation, the first argument has to be a file handle created by CreateFile and nothing else.

Thus you need something like this:

HANDLE fileHandle = CreateFile(
  FileName, //LPCTSTR
  GENERIC_READ,
  FILE_SHARE_READ,
  NULL,
  OPEN_EXISTING,
  FILE_ATTRIBUTE_NORMAL,
  NULL
);

if ( fileHandle != INVALID_HANDLE )
{
    GetFileTime( fileHandle, NULL, NULL, &lastWriteTime );
    CloseHandle( fileHandle );
}
else
{
    // error, do something else...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • 1
    If you don't want to open the file, you can use `FindFirstFile()` (wrapped by C++Builder's `Sysutils::FindFirst()` function) or `GetFileAttributesEx()` instead. – Remy Lebeau Jul 23 '15 at 21:47