Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: ftLastaccess


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 8 - Last Post: Jun 25, 2014 1:04 PM Last Post By: Andrew McIsaac
Andrew McIsaac

Posts: 93
Registered: 3/12/13
ftLastaccess  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2014 8:32 AM
Good morning,
Usng C++,Windows 7

I am having difficulties with Time :)

Trying to display attributes of a file on a drive.
Looking for Creation Date, Modification Date, and Access Date.

Have Tried :
struct stat buf;
AnsiString filename;
char timebuf[9];
char timeStr[100];
filename=L"C:
some file.bmp";
ifstream filestr (filename.c_str());
if(filestr.good())
{
// ShowMessage("File OK");
if(!stat(filename.c_str(), &buf))
{
}
_strtime(timebuf);
strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_ctime));
Memo1->Lines->Add(timeStr);

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_mtime));
Memo1->Lines->Add(timeStr);

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_mtime));
Memo1->Lines->Add(timeStr);

}
else
{
ShowMessage("File Not Found");
}

This works well, except for the accessed, it is not correct, will be the same as modified.
sample output : 06-19-2014 10:23:47

I can see that if I use
ftLastaccess, it is different than modified, but the date format is not something I can use,

So Far :
HANDLE hFile;
FILETIME ftCreation, ftLastaccess, ftLastwrite;
SYSTEMTIME stCreation, stLastaccess, stLastwrite;
hFile = CreateFile(L"C:
some file.bmp", GENERIC_READ,
FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

Memo1->Lines->Add(ftLastaccess.dwLowDateTime);
Memo1->Lines->Add(ftLastaccess.dwHighDateTime);

sample output :17331632, 1635620
Anyone know how to convert to something more like the first example ?

Thanks in advance,
Andrew
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: ftLastaccess  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2014 9:43 AM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Andrew wrote:

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_mtime));
Memo1->Lines->Add(timeStr);
strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_mtime));
Memo1->Lines->Add(timeStr);
<snip>
This works well, except for the accessed, it is not correct, will be
the same as modified. sample output : 06-19-2014 10:23:47

Look at that code again more carefully. You are not outputting the buf.st_atime
field at all, you are outputting the buf.st_mtime field twice instead. That
is why the values are the same.

I can see that if I use ftLastaccess, it is different than modified,
but the date format is not something I can use

Use the Win32 API FileTimeToLocalFileTime() and FileTimeToSystemTime() functions:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724277.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724280.aspx

Also, look at the RTL's Sysutils::SystemTimeToDateTime() function:

http://docwiki.embarcadero.com/Libraries/XE6/en/System.SysUtils.SystemTimeToDateTime

hFile = CreateFile(L"C:
some file.bmp", GENERIC_READ,
FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Memo1->Lines->Add(ftLastaccess.dwLowDateTime);
Memo1->Lines->Add(ftLastaccess.dwHighDateTime);

You need to call the Win32 API GetFileTime() function after calling CreateFile()
and before outputting the values:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724320.aspx

Alternatively, if you don't want to open the file, you can use the Win32
API FindFirstFile() function:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418.aspx

Or the RTL's FindFirst() function:

http://docwiki.embarcadero.com/Libraries/XE6/en/System.SysUtils.FindFirst

In the latter case, you can either:

1. use the date/time values from the TSearchRec.FindData field

2. pass the TSearchRec.Time to the RTL's FileDateToDateTime() function:

http://docwiki.embarcadero.com/Libraries/XE6/en/System.SysUtils.FileDateToDateTime

3. in XE and later only, use the TSearchRec.TimeStamp property, which returns
a TDateTime.

--
Remy Lebeau (TeamB)
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: ftLastaccess  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2014 10:10 AM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Thanks,

Sorry for the typo,
if
strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_atime));
Memo1->Lines->Add(timeStr);
I had mtime twice.
with atime, returns the created date.

Lots to learn here.

thank you for the help.
I will now try to understand this further.

Thank you.
Andrew

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: ftLastaccess  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2014 10:47 AM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Andrew wrote:

with atime, returns the created date.

If ftCreation, ftLastaccess, and ftLastwrite are different values, they cannot
resolve to the same time_t values in the stat structure. Look at the RTL's
source code for stat() in the $(BDS)\source\cpprtl\Source\io\stat.c file,
and _statcvt() in the $(BDS)\source\cpprtl\Source\time\statcvt.c file.

--
Remy Lebeau (TeamB)
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: ftLastaccess  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2014 12:10 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks for all help so far,
Still having some difficulties.
Simplified code,

this will show access date correctly ( changed file name )

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_atime));
Memo1->Lines->Add(timeStr);

If I run this on an old file, sees the access date correctly,
then open the file,
access date changes to today, and will have correct time,
wait,
reopen file,
access time does not change ? still shows initial of today.

Is there something else I'm missing, or is this a windows / server issue ?

Other issues :

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_ctime));
Memo1->Lines->Add(timeStr);

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_mtime));
Memo1->Lines->Add(timeStr);

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_atime));
Memo1->Lines->Add(timeStr);

where ctime: created, mtime : modified, atime: accessed

Output:
Memo1
06-16-2014 09:17:26
06-12-2014 14:22:37
06-16-2014 09:17:26

Modified before created ?

Thanks again,
Andrew

Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: ftLastaccess  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2014 12:41 PM   in response to: Andrew McIsaac in response to: Andrew McIsaac
El 25/06/2014 21:10, Andrew McIsaac escribió:

Other issues :

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_ctime));
Memo1->Lines->Add(timeStr);

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_mtime));
Memo1->Lines->Add(timeStr);

strftime(timeStr,100,"%m-%d-%Y %H:%M:%S",localtime(&buf.st_atime));
Memo1->Lines->Add(timeStr);

where ctime: created, mtime : modified, atime: accessed

Output:
Memo1
06-16-2014 09:17:26
06-12-2014 14:22:37
06-16-2014 09:17:26

Modified before created ?

That means that the creation date is the date when the file was copied
from another location to the actual folder. The modified date remains
the same as the original file.

Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: ftLastaccess  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2014 12:45 PM   in response to: Antonio Estevez in response to: Antonio Estevez
that makes sense,

Thanks,
Andrew
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: ftLastaccess  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2014 12:51 PM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Andrew wrote:

If I run this on an old file, sees the access date correctly,
then open the file, access date changes to today, and will
have correct time, wait, reopen file, access time does not
change ? still shows initial of today.

That is normal behavior for Windows, and is documented on MSDN:

File Times
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724290.aspx

Not all file systems can record creation and last access times, and not all
file systems record them in the same manner. For example, the resolution
of create time on FAT is 10 milliseconds, while write time has a resolution
of 2 seconds and access time has a resolution of 1 day, so it is really the
access date. The NTFS file system delays updates to the last access time
for a file by up to 1 hour after the last access.

--
Remy Lebeau (TeamB)
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: ftLastaccess  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 25, 2014 1:04 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you for all your help and no expletives :)

So much to learn.

Thanks,
Andrew
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02