Solved

Getting and comparing file dates/times

Posted on 1998-11-18
5
238 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 20 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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 accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

623 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