Search code examples
c++winapidatefiletime

How to comapre 2 FILETIME variables to see if a file is older than 30 days


I would like to check if a folder is greater that 30 days old and have the following code

static bool ExpiredDirectory(CComBSTR directory)
{
    WIN32_FILE_ATTRIBUTE_DATA fileAttrData = {0};
    GetFileAttributesEx(directory, GetFileExInfoStandard, &fileAttrData);

    FILETIME ftCreatedDate = fileAttrData.ftCreationTime;
    FILETIME now;
    SYSTEMTIME nowst;
    ULARGE_INTEGER t1, t2;
    GetSystemTime(&nowst);
    SystemTimeToFileTime(&nowst, &now);

      ---compare code goes here

}

I want to compare the variables above to see if ftCreatedDate is greater than 30 days old


Solution

  • ive fixed the problem with help from 30 days Difference on SYSTEMTIME. Turns out this guy wanted to do almost the same. Should have seen this before

    My code is now

    WIN32_FILE_ATTRIBUTE_DATA fileAttrData = {0};
    GetFileAttributesEx(directory, GetFileExInfoStandard, &fileAttrData);
    FILETIME ftCreationTime = fileAttrData.ftCreationTime;
    FILETIME now;
    SYSTEMTIME nowst;
    ULARGE_INTEGER t1, t2;
    
    GetSystemTime(&nowst);
    SystemTimeToFileTime(&nowst, &now);
    
    memcpy(&t1, &ftCreationTime, sizeof(t1));
    memcpy(&t2, &now, sizeof(t1));
    ULONGLONG diff = (t1.QuadPart<t2.QuadPart)?(t2.QuadPart-t1.QuadPart):(t1.QuadPart-t2.QuadPart);
    
    if(diff>30*24*60*60*(ULONGLONG)10000000)
    {
        return true;
    }
    
    return false;