Solved

Getting and comparing file dates/times

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio Exe Issue, 12 40
Access Query (QBE) Using VBA Function for Criteria 13 72
VS2015 compilation and missing DLLs 9 193
How to copy an image file into clipboard C/C++? 1 252
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…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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