Solved

Getting and comparing file dates/times

Posted on 1998-11-18
5
233 Views
Last Modified: 2013-12-03
Needed:  NT C source to get file (c:\aaa\bbb\c.ext) date/time and see if it is over xyz number of hours old since last read/write.
0
Comment
Question by:b9
5 Comments
 
LVL 1

Expert Comment

by:perrizo
Comment Utility
This isn't the C but if your using MFC CFile you only have to call the CFile member function:

GetStatus(LPCTSTR lpszFileName, CFileStatus& rStatus );

The CFileStatus class has the following member variables:

-CTime m_ctime   The date and time the file was created.
-CTime m_mtime   The date and time the file was last modified.
-CTime m_atime   The date and time the file was accessed for read
-LONG m_size   The logical size of the file in bytes
-BYTE m_attribute   The attribute byte of the file.
-char m_szFullName[_MAX_PATH]   The absolute filenam

I'm not sure about the true C source for it though?  sorry.
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
Lookup _stat in MSVC help, example:

/* STAT.C: This program uses the _stat function to
 * report information about the file named STAT.C.
 */

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

void main( void )
{
   struct _stat buf;
   int result;
   char buffer[] = "A line to output";

   /* Get data associated with "stat.c": */
   result = _stat( "stat.c", &buf );

   /* Check if statistics are valid: */
   if( result != 0 )
      perror( "Problem getting information" );
   else
   {
      /* Output some of the statistics: */
      printf( "Time modified : %s", ctime( &buf.st_atime ) );
   }
}

0
 
LVL 11

Accepted Solution

by:
mikeblas earned 20 total points
Comment Utility
Here's some code that actually does what you've asked for within the requirements you've provided.


/*
 * compile with
 *   cl older.c
*/

#include <windows.h>
#include <oleauto.h>
#include <stdio.h>

#pragma comment(lib, "oleaut32.lib")

BOOL CheckAge(LPCTSTR pstrFileName, int nAgeHours, BOOL* pbResult)
{
   BOOL bRetVal = TRUE;
   HANDLE hFile;

   hFile = CreateFile(pstrFileName, GENERIC_READ,
      FILE_SHARE_READ | FILE_SHARE_WRITE,
      NULL, OPEN_EXISTING, 0, NULL);

   if (hFile != INVALID_HANDLE_VALUE)
   {
      double dTimeFile;
      double dTimeNow;

      FILETIME ftimeFile;
      SYSTEMTIME stimeFile;
      SYSTEMTIME stimeNow;

      GetSystemTime(&stimeNow);
      GetFileTime(hFile, NULL, NULL, &ftimeFile);
      CloseHandle(hFile);

      FileTimeToSystemTime(&ftimeFile, &stimeFile);

      SystemTimeToVariantTime(&stimeFile, &dTimeFile);
      SystemTimeToVariantTime(&stimeNow, &dTimeNow);

      if ((dTimeNow - dTimeFile) <= (nAgeHours / 24.0))
         *pbResult = FALSE;
      else
         *pbResult = TRUE;
   }
   else
      bRetVal = FALSE;

   return bRetVal;
}

void TestCheckAge(LPCTSTR pstr, int nAgeHours)
{
   BOOL bResult;

   if (CheckAge(pstr, nAgeHours, &bResult))
   {
      printf("Check Age Worked!\n   File %s is", pstr);

      if (!bResult)
         printf(" not");

      printf(" older than %d hours.\n\n", nAgeHours);
   }    
   else
      {
      printf("Check Age failed!\n");
            printf("   File %s didn't work\n\n", pstr);
      }
}

void main()
{
   TestCheckAge("C:\\AUTOEXEC.BAT", 10);
   TestCheckAge("C:\\CONFIG.SYS", 10);
   TestCheckAge("C:\\ThisFileDoesNotExist.TXT", 35);
   TestCheckAge("older.exe", 35);
}


B ekiM
0
 

Author Comment

by:b9
Comment Utility
Excellent.  Thank you for the quick response time as well.  SystemTimeToVariantTime is undocumented?
0
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
It's documented in the copy of MSDN that I have.

B ekiM
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now