Link to home
Start Free TrialLog in
Avatar of bert1
bert1

asked on

Compare filetime and systemtime

I have a filetime and want to compare that to the (systemtime - 7 days) and see which one is the oldest. How do I do that ?

Thanks!
Avatar of KangaRoo
KangaRoo

You can use difftime() to calculate the difference (in seconds) between two time_t values.
There are no standards on obtaining the file time, so we'll need to know the OS and compiler you're targetting/using.
Avatar of bert1

ASKER

Im using Borland C++ 5.02.
Then you can use getftime() to obtain the file time:

#include <stdio.h>
#include <io.h>
#include <time.h>
#include <dos.h>
#include <conio.h>

int main(void)
{
   time_t now;
   FILE *stream;
   struct ftime ft;

   clrscr();

   if ((stream = fopen("TEST.$$$",
        "rt")) == NULL)
   {
      fprintf(stderr, "Cannot open file.\n");
      return 1;
   }

   getftime(fileno(stream), &ft);

   now = time(NULL);  /* Gets system
                           time */

   printf("The difference is: %f seconds\n",difftime(now, ft.time));
   getch();

   fclose(stream);
   return 0;
   return 0;
}

Avatar of bert1

ASKER

I have used

GetFileTime( hFile, &CreationTime, &LastAccessTime, &LastWriteTime );

to get the filetime ... how do I compare that to the SYSTEMTIME - 7 days ??

 


Avatar of bert1

ASKER

I want to subtract 7 days from the system time!

//bert1
7 days is 7 * 24 * 60 * 60 =  604800 seconds
if(difftime(now, ft.time) > 604800)
  printf("The file is more then 7 days old");

Avatar of bert1

ASKER

Im using GetFileTime and SystemTimeToFileTime (winAPI). Not dos!

And I want to subtract a value from the filetime (created) ... -> I need to convert filetime to large integer ... and then subtract , but how do I convert it to large integer?

GetFileTime( hFile, &CreationTime, &LastAccessTime, &LastWriteTime )
Actually you are too late with that infornation. Your Q stated that you wanted to compare a file time with the system time. When I asked about what OS and compiler you only stated you were using BC5, not that you would insist upon using Win API. Please be more specific about that in the future!

I suspect you can simply cast the FILETIME struct to a LARGE_INTEGER, they have the same layout.

Avatar of bert1

ASKER

Im sorry for not being that detailed ... Im new to C++ :-\
I can give you the points ... for the last comment ...
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kangroo is right.  He put a lot of effort into this question and it was all wasted because you didn't provide all the necessary information.  This is the C++ topic area, not the windows topic area.  A C++ solution, like he offered, is appropriate.  To later require a windows solution is unfair.

Anyways you can convert a FILETIME  to a 64 bit value using

int64 Time = (FT.dwHighDateTime << 32) +  FT.dwLowDateTime;
opps.  Sorry for my comment at this time.  I was trying to post if for a while, but the page would not accept it.  Now it is clearly out of place.