4
\$\begingroup\$

I have an MFC application where I need to determine if a file's last write time is older than n hours.

I thought there was a method to get total seconds from either system or file time, but I ended up coding this method which works, but there might be a leaner method.

bool FileOlderThanHours(HANDLE hFile, int hours)
{
 FILETIME ftCreate, ftLastAccess, ftLastWrite;
 SYSTEMTIME stNow, stFile;
 GetSystemTime(&stNow); 
 if (!GetFileTime(hFile, &ftCreate, &ftLastAccess, &ftLastWrite))
 return false;
 if (!FileTimeToSystemTime(&ftLastWrite, &stFile))
 return false;
 COleDateTime oleNow(stNow.wYear, stNow.wMonth, stNow.wDay, stNow.wHour, 
 stNow.wMinute, stNow.wSecond);
 COleDateTime oleFile(stFile.wYear, stFile.wMonth, stFile.wDay, stFile.wHour, 
 stFile.wMinute, stFile.wSecond);
 COleDateTimeSpan timeDiff = oleNow - oleFile;
 double totHours = timeDiff.GetTotalHours();
 return (totHours > hours);
}

If someone knows a leaner way, please let me know, thanks.

user673679
12.2k2 gold badges34 silver badges65 bronze badges
asked Dec 30, 2021 at 15:36
\$\endgroup\$
4
  • \$\begingroup\$ You seem to be missing the return type on the function. \$\endgroup\$ Commented Dec 30, 2021 at 17:11
  • \$\begingroup\$ my bad--I copied this method from my class and omitted the type along with the ... ClassName:: prefix. \$\endgroup\$ Commented Dec 30, 2021 at 18:21
  • 1
    \$\begingroup\$ Please don't edit or append code in the question after it has been reviewed. For details, see: codereview.meta.stackexchange.com/questions/1763/… \$\endgroup\$ Commented Jan 4, 2022 at 19:06
  • \$\begingroup\$ In addition to what user673679 stated: incorporating feedback from answers goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see What should I do when someone answers my question? as well as what you may and may not do after receiving answers. \$\endgroup\$ Commented Jan 4, 2022 at 19:13

1 Answer 1

3
\$\begingroup\$

In MFC, the CFile::GetStatus method lets you query the creation, modification or access time of a file.

Code to use it would look something like:

CTime getModificationTIme(const LPCTSTR filename)
/* Returns the last modification time of the requested file. The
 * filename must be a null-terminated wide string.
 */
{
 CFileStatus inputFileStatus;
 if (!CFile::GetStatus( filename, inputFileStatus )) {
 // Handle the error, perhaps by throwing an exception. E.g.,
 THROW((CException*) new CCustomException(filename));
 }
 return inputFileStatus.m_mtime;
}

In current versions of Windows, the filename is a null-terminated wide string. Future versions might un-deprecate the char* version to accept a UTF-8 filename.

Visual Studio 2017 and up support the standard library’s <filesystem> header, of which you can find an example of usage at cppreference.

One major difference between Windows and other systems is that, on Windows, the <filesystem> API uses wide strings to create a std::filesystem::path.

answered Dec 30, 2021 at 20:09
\$\endgroup\$
2
  • \$\begingroup\$ Thanks! I implemented the CTime class into my method. \$\endgroup\$ Commented Dec 30, 2021 at 21:57
  • \$\begingroup\$ @netcat Glad it helped! Anyone who copies this later should be sure to notice, my error-handling code was just a dummy. \$\endgroup\$ Commented Dec 30, 2021 at 22:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.