1  /* filedate.c v1.0 - 2011-02-06
  2  The program displays or compares the dates of two files.
  3  Update: http://myc01.free.fr/filedate
  4  
  5  License: Public Domain Dedication (CC0 1.0)
  6    http://creativecommons.org/publicdomain/zero/1.0/
  7  
  8  Usage: filedate.exe /[ACWS] ref_file [test_file]
  9    A : last Access time
 10    C : Creation time
 11    W : last Write time (defaut)
 12    S : file Size
 13    Date format: Hour:Minute:Second.Milliseconds  Day/Month/Year
 14  
 15  Examples:
 16    filedate.exe /W test1.txt
 17    filedate.exe /W test1.txt test2.txt
 18  */
 19  
 20  #include <windows.h>
 21  #include <stdio.h>
 22  #include <stdlib.h>
 23  #include <ctype.h>
 24  
 25  #define PROGRAM "Filedate"
 26  #define VERSION "1.0"
 27  #define DATE    "06/02/2011"
 28  
 29  #define MAXDWORD64 4294967296ULL    /* = ((uint64)MAXDWORD)+1 */
 30  typedef unsigned long long uint64;  /* = unsigned __int64     */
 31  
 32  FILETIME GetFileLastWriteTime (char *name, char test)
 33  {
 34      FILETIME size = {0, 0};
 35      WIN32_FIND_DATA FindFileData;
 36      HANDLE hFind;
 37  
 38      if ( (hFind = FindFirstFile (name, &FindFileData)) == INVALID_HANDLE_VALUE)
 39      {
 40          puts ("Error: file not found (3).");
 41          exit (3);
 42      }
 43      else
 44          FindClose (hFind);
 45  
 46      if (test == 'A') return FindFileData.ftLastAccessTime;
 47      if (test == 'C') return FindFileData.ftCreationTime;
 48      if (test == 'W') return FindFileData.ftLastWriteTime;
 49      if (test == 'S')
 50      {
 51          size.dwLowDateTime  = FindFileData.nFileSizeLow;
 52          size.dwHighDateTime = FindFileData.nFileSizeHigh;
 53      }
 54      return size;
 55  }  /* GetFileLastWriteTime */
 56  
 57  void PrintMessage (void)
 58  {
 59      puts (PROGRAM" - v"VERSION" ("DATE")");
 60      puts ("Usage: filedate.exe /[ACWS] ref_file [test_file]");
 61      puts ("  A = last Access time");
 62      puts ("  C = Creation time");
 63      puts ("  W = last Write time (defaut)");
 64      puts ("  S = file Size");
 65      puts ("  Date format: Hour:Minute:Second.Milliseconds  Day/Month/Year");
 66      exit (2);
 67  }  /* PrintMessage */
 68  
 69  int main (int argc, char *argv[])
 70  {
 71      int nResult = 0;
 72      char test;
 73      SYSTEMTIME st;
 74      FILETIME ft1, ft2;
 75      uint64 L;
 76  
 77      if (argc                      <=  2 ) PrintMessage ();
 78      if (strlen ( (char*) argv[1]) !=  2 ) PrintMessage ();
 79      if (*argv[1]                  != '/') PrintMessage ();
 80  
 81      test = toupper (* (argv[1] + 1));
 82      if ( (test != 'A') && (test != 'C') &&
 83              (test != 'W') && (test != 'S')) PrintMessage ();
 84  
 85                    ft1 = GetFileLastWriteTime (argv[2], test);
 86      if (argc > 3) ft2 = GetFileLastWriteTime (argv[3], test);
 87  
 88      if (test != 'S')
 89      {
 90          FileTimeToLocalFileTime (&ft1, &ft1);
 91          FileTimeToSystemTime    (&ft1, &st );
 92  
 93          if (argc > 3)
 94              printf ("A= ");
 95  
 96          printf ("%02d:%02d:%02d.%03d  %02d/%02d/%04d\n",
 97              st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, st.wDay, st.wMonth, st.wYear);
 98  
 99          if (argc > 3)
100          {
101              FileTimeToLocalFileTime (&ft2, &ft2);
102              FileTimeToSystemTime    (&ft2, &st );
103              printf ("B= %02d:%02d:%02d.%03d  %02d/%02d/%04d\n",
104                  st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, st.wDay, st.wMonth, st.wYear);
105          }
106      }
107      else
108      {
109          if (argc > 3)
110              printf ("A= ");
111  
112          L = (ft1.dwHighDateTime * MAXDWORD64) + ft1.dwLowDateTime;
113          printf ("%I64u\n", L);
114  
115          if (argc > 3)
116          {
117              L = (ft2.dwHighDateTime * MAXDWORD64) + ft2.dwLowDateTime;
118              printf ("B= %I64u\n", L);
119          }
120      }
121  
122      if (argc > 3)
123      {
124          nResult = CompareFileTime (&ft1, &ft2);
125  
126          printf ("Test= %c : ", test);
127  
128               if (nResult == -1) puts ("A < B (-1)");
129          else if (nResult ==  0) puts ("A = B (0)");
130          else if (nResult ==  1) puts ("A > B (1)");
131      }
132  
133      return nResult;
134  }  /* main */