Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting and comparing file dates/times

Posted on 1998-11-18
5
Medium Priority
?
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:perrizo
ID: 1416186
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
ID: 1416187
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 80 total points
ID: 1416188
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
ID: 1416189
Excellent.  Thank you for the quick response time as well.  SystemTimeToVariantTime is undocumented?
0
 
LVL 11

Expert Comment

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

B ekiM
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

718 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