comparing dates in C - need help

jaggar2
jaggar2 used Ask the Experts™
on
Hi, I can not seem to find how to compare the current date to the date of a file in windows.  Object is to see if a file in a directory is older then a specific time and delete it if so. But I dont know how to compare the date to do so: Below is code I got so far, any ideas?

int GetLastWriteTime(char *sFilePath, LPSTR lpszString)
{
   HANDLE hFile2;
   FILETIME ftCreate, ftAccess, ftWrite;
   SYSTEMTIME stUTC, stLocal;
     
   /* Create a handle to access the file */
     hFile2 = CreateFile(sFilePath,               /* Open File*/
     GENERIC_READ,     /* Open for reading  */
     FILE_SHARE_READ,/* Share for reading     */
     NULL,          /* No Security     */
     OPEN_EXISTING,     /* Existing file only     */
     FILE_ATTRIBUTE_NORMAL,     /* Normal file     */
        NULL);     /* No attr. template     */

     /* Check the return value for errors */
     if (hFile2 == INVALID_HANDLE_VALUE) {
          printf("ERROR\n");
          return(0);
     }
     else {
          // Retrieve the file times for the file.
        if (!GetFileTime(hFile2, &ftCreate, &ftAccess, &ftWrite))
         return FALSE;
         // Convert the last-write time to local time.
         FileTimeToSystemTime(&ftWrite, &stUTC);
         SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
         // Build a string showing the date and time.
         wsprintf(lpszString, "%02d/%02d/%d  %02d:%02d", stLocal.wDay, stLocal.wMonth, stLocal.wYear, stLocal.wHour, stLocal.wMinute);

         /* Close handle */
         CloseHandle(hFile2);

   }
         
   return(0);
}


int main()
{
     char sFilePath[100]     = "C:\\Error\\Cases\\*";
     char FullPath[100] = "C:\\Error\\Cases\\";
     char str[100] = {'\0'};
     char FileDate[100] = {'\0'};
     char Today[100] = {'\0'};
     char Search1[10] = ".";
     char Search2[10] = "..";
     int iCount               = 0;
     int num = 0;
     WIN32_FIND_DATA ds;
     HANDLE hFile;
     SYSTEMTIME st;

     GetLocalTime(&st);
     sprintf(Today, "%02d/%02d/%d  %02d:%02d", st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute);

    hFile = FindFirstFile(sFilePath, &ds);
     if(hFile == INVALID_HANDLE_VALUE) {
          return(0);
     }
     do
     {    
          if ( (strcmp(ds.cFileName, Search1)!=0) && (strcmp(ds.cFileName, Search2)!=0) )
          {
               iCount = iCount + 1;
         
               strcpy(str, "");
               strcat(str, FullPath);
               strcat(str, ds.cFileName);

               GetLastWriteTime(str, FileDate);

               printf("Today: %s --", Today);

               printf("File Date: %d\n", FileDate);
                        // how can i compare the two dates

          }

     } while(FindNextFile(hFile, &ds) == TRUE);

     FindClose(hFile);    

     return(0);    
}


Basically what it comes down to is the following:

I get the current date/time in  whatever format
I get the file date/time in whatever format

now is there any way I can get the difference between them using a c standard or a existing function?

for example:  datediff = GetDateDiff(CurrentTime, FileTime)

So datediff would be the difference of time like in 7 days or something like that, it could be in days, hours, even seconds.

Any way to get a date/time deference between two is what it comes down too. My previous code could be confusing.
Thanks,

 
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Could you post all of the code?

>> printf("File Date: %d\n", FileDate);

If you can display it as a double why don't you just convert it to one and use the logical > operator in an if statement.

Author

Commented:
Basically what it comes down to is the following:

I get the current date/time in  whatever format
I get the file date/time in whatever format

now is there any way I can get the difference between them using a c standard or a existing function?

for example:  datediff = GetDateDiff(CurrentTime, FileTime)

So datediff would be the difference of time like in 7 days or something like that, it could be in days, hours, even seconds.

Any way to get a date/time deference between two is what it comes down too. My previous code could be confusing.
 Thanks,

Commented:
Do it the other way around:

First, convert the system time to FILETIME.  Use either GetSystemTimeAsFileTime() or SystemTimeToFileTime().

The FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

Now, copy (or cast) the FILETIMEs to a 64-bit unsigned integer (ULARGE_INTEGER or better - __uint64) and subtract them.  Divide the result by 10000000 to get the difference in seconds.
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to alexo

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial