Solved

GetCreationTime, GetLastAccessTime, GetLastWriteTime

Posted on 2003-12-09
4
1,736 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
ID: 9908601
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
ID: 9909416
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
ID: 9911377
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
ID: 9911462
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Smart Camera scanning and reading information 3 120
Window placement 17 95
post4 challenge 28 113
Capture logon name 13 100
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.

713 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