Solved

GetCreationTime, GetLastAccessTime, GetLastWriteTime

Posted on 2003-12-09
4
1,684 Views
Last Modified: 2013-11-20
My code is dying at run-time.  The output displays the filename but nothing else (it bombs after there).  What am I doing wrong?  (Note: I'm looking for the simplest solution that builds upon this little block below, which is merely attempting to display the filename, and related time information).  Thanks.

#include <afx.h>
#include <iostream>
using namespace std;

void main () {
      CFileFind finder;
      CTime creation_time;
      BOOL bWorking = finder.FindFile("C:\\send.bat");
      while (bWorking)
      {
      bWorking = finder.FindNextFile();
      cout << (LPCTSTR) finder.GetFileName() << endl;
        cout << (LPCTSTR) finder.GetCreationTime(creation_time) << endl;
        cout << (LPCTSTR) finder.GetLastAccessTime(creation_time) << endl;
        cout << (LPCTSTR) finder.GetLastWriteTime(creation_time) << endl;
      }
}
0
Comment
Question by:MrSnood
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
The GetCreationTime() method returns a bool, and not a CString. You are casting the bool to a LPCTSTR, which of course points to the memory address 0 or 1. That's the reason you are getting a crash.
0
 

Author Comment

by:MrSnood
Comment Utility
OK, thank you, but how do I cout the stuff I'm actually looking for (the date/times)?  
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 50 total points
Comment Utility
You need to convert the CTime object to a string first:

finder.GetCreationTime(creation_time);
CString sTime = creation_time.Format( ...your format string ... );
cout << sTime << endl;

Look up the description for CTime::Format() for information about how to format your time string.
0
 
LVL 1

Expert Comment

by:luvjd
Comment Utility
Hi Snood..
Check this example...

 CFileFind finder;

   BOOL bWorking = finder.FindFile(_T("C:\\*.*"));

   while (bWorking)
   {
      bWorking = finder.FindNextFile();

      _tprintf(_T("%s\n\t"), (LPCTSTR) finder.GetFileName());
      _tprintf(_T("%c"), finder.IsArchived() ? 'A' : 'a');
      _tprintf(_T("%c"), finder.IsCompressed() ? 'C' : 'c');
      _tprintf(_T("%c"), finder.IsHidden() ? 'H' : 'h');
      _tprintf(_T("%c"), finder.IsNormal() ? 'N' : 'n');
      _tprintf(_T("%c"), finder.IsReadOnly() ? 'R' : 'r');
      _tprintf(_T("%c"), finder.IsSystem() ? 'S' : 's');
      _tprintf(_T("%c"), finder.IsTemporary() ? 'T' : 't');

      _tprintf(_T("\t%I64u byte(s)\n"), finder.GetLength());
     
      CTime tempTime;
      CString str;
     
      _tprintf(_T("\tCreated    : "));
      if (finder.GetCreationTime(tempTime))
      {
         str = tempTime.Format(_T("%c"));
         _tprintf(_T("%s\n"), (LPCTSTR) str);
      }
      else
         _tprintf(_T("(unavailable)\n"));

      _tprintf(_T("\tLast Access: "));
      if (finder.GetLastAccessTime(tempTime))
      {
         str = tempTime.Format(_T("%c"));
         _tprintf(_T("%s\n"), (LPCTSTR) str);
      }
      else
         _tprintf(_T("(unavailable)\n"));

      _tprintf(_T("\tLast Write : "));
      if (finder.GetLastWriteTime(tempTime))
      {
         str = tempTime.Format(_T("%c"));
         _tprintf(_T("%s\n"), (LPCTSTR) str);
      }
      else
         _tprintf(_T("(unavailable)\n"));

      _tprintf(_T("\n"));
   }

Luv..
Jd
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now