Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get File Last Modify Time and Compare

Tags:

c++

visual-c++

I want a piece of function which will take a file and last how many days, if it was older than that date, will return 0 otherwise 1... Something like that...

For example:

int IsOlder(TCHAR *filename, int days)
{

do operation.

If last modify date was older than days variable
return 0
else
return 1

}

It's MS VC++ 6 for Windows. Thanks from now!

like image 675
Fraklin Avatar asked Dec 21 '09 08:12

Fraklin


1 Answers

Windows has an API function called GetFileTime() (doc on MSDN) taking a file handle in parameter and 3 FILETIME structures to be filled with date-time info:

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

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

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

Then you can use fields wYear, wMonth, etc. to compare with your number of days.

like image 58
Julien-L Avatar answered Nov 02 '22 12:11

Julien-L