开发者

Get File Last Modify Time and Compare

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 operati开发者_JS百科on.

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

}

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


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.


GetFileTime gets the various dates relevant to a file. There's an example.

You will need to fetch the last write time, and calculate the difference in days from there. As the GetFileTime function returns the quite unwieldy FILETIME structure you probably want to convert it into system time (struct SYSTEMTIME) with FileTimeToSystemTime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜