Get the time the file was last modified and compare it

I need a part of a function that takes a file and lasts how many days, if it was older than this date, it will return 0, otherwise 1 ... Something like this ...

For example:

int IsOlder(TCHAR *filename, int days) { do operation. If last modify date was older than days variable return 0 else return 1 } 

This is MS VC ++ 6 for Windows. Thank you now!

+6
c ++ visual-c ++
source share
2 answers

Windows has an API function called GetFileTime() ( doc on MSDN ), taking the file descriptor in the parameter and 3 FILETIME structures that will be with date and time information:

 FILETIME creationTime, lpLastAccessTime, lastWriteTime; bool err = GetFileTime( h, &creationTime, &lpLastAccessTime, &lastWriteTime ); if( !err ) error 

The FILETIME structure FILETIME obfuscated, use the FileTimeToSystemTime() function to translate it into the SYSTEMTIME structure, which is easier to use:

 SYSTEMTIME systemTime; bool res = FileTimeToSystemTime( &creationTime, &systemTime ); if( !res ) error 

Then you can use the fields wYear , wMonth , etc. for comparison with your number of days.

+12
source share

GetFileTime gets various dates related to the file. There is an example .

You will need to get the latest recording time and calculate the difference in days from there. Since the GetFileTime function returns a rather cumbersome FILETIME structure, which you probably want to convert to system time ( struct SYSTEMTIME ) using FileTimeToSystemTime .

+2
source share

All Articles