Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

Getting and comparing file dates/times

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
b9
Asked:
b9
1 Solution
 
perrizoCommented:
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
 
Answers2000Commented:
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
 
mikeblasCommented:
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
 
b9Author Commented:
Excellent.  Thank you for the quick response time as well.  SystemTimeToVariantTime is undocumented?
0
 
mikeblasCommented:
It's documented in the copy of MSDN that I have.

B ekiM
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now