BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to effectively modify the last access time of a file

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
JF Jolin
Guest





PostPosted: Thu Apr 05, 2007 8:10 am    Post subject: How to effectively modify the last access time of a file Reply with quote



Here is the scenario:

hFile = CreateFile( lpszFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );

SetFileTime( hFile, &fTimeCreate, &fTimeLastAccess, &fTimeLastWrite );

CloseHandle( hFile );


Whatever time is specified in fTimeLastAccess, that will be overwritten when CloseHandle instruction will be executed.


__
JF Jolin
Back to top
Koen
Guest





PostPosted: Thu Apr 05, 2007 2:06 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote



JF Jolin wrote:

Quote:
hFile = CreateFile( lpszFileName, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );

SetFileTime( hFile, &fTimeCreate, &fTimeLastAccess, &fTimeLastWrite );

CloseHandle( hFile );

When I use your code, I the last access time is adjusted correctly. Are you
sure you don't open the file elsewhere?
Back to top
Mark Jacobs
Guest





PostPosted: Thu Apr 05, 2007 5:25 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote



"Koen" <koen (AT) ha (DOT) com> wrote in message
news:Xns990966E8E657Fkoenhacom (AT) 207 (DOT) 105.83.66...
Quote:
JF Jolin wrote:
hFile = CreateFile( lpszFileName, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
SetFileTime( hFile, &fTimeCreate, &fTimeLastAccess, &fTimeLastWrite );
CloseHandle( hFile );

When I use your code, I the last access time is adjusted correctly. Are you
sure you don't open the file elsewhere?

No, he means that the CloseHandle call sets the last access time to when the
CloseHandle happened! I don't see a way round this! Certainly, the other 2
work fine (last modified and last created), but last access includes the
access required to set the last access date and time - a bit of a Catch 22
situation!
--
Mark Jacobs
DK Computing
http://www.dkcomputing.co.uk
Back to top
Bob Gonder
Guest





PostPosted: Thu Apr 05, 2007 7:08 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

"Mark Jacobs" <http://www.jacobsm.com/mjmsg.htm?BorlandNG> wrote:

Quote:
No, he means that the CloseHandle call sets the last access time to when the
CloseHandle happened! I don't see a way round this! Certainly, the other 2
work fine (last modified and last created), but last access includes the
access required to set the last access date and time - a bit of a Catch 22
situation!

Change the system time before closing ?
Back to top
Ed Mulroy
Guest





PostPosted: Thu Apr 05, 2007 7:13 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

You are probably correct in this but there is one other thing he might try.
I've been doing since the DOS days - do a flush on the file immediately
prior to the time set and close. For Win32 it goes something like this:

FlushFileBuffers(hFile);
SetFileTime(--arg list---);
CloseHandle(hFile);

.. Ed

Quote:
Mark Jacobs wrote in message
news:4614eacb (AT) newsgroups (DOT) borland.com...

When I use your code, I the last access time is adjusted correctly. Are
you sure you don't open the file elsewhere?

No, he means that the CloseHandle call sets the last access time to when
the CloseHandle happened! I don't see a way round this! Certainly, the
other 2 work fine (last modified and last created), but last access
includes the access required to set the last access date and time - a bit
of a Catch 22 situation!
Back to top
Ed Mulroy
Guest





PostPosted: Thu Apr 05, 2007 7:18 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

I just tried it with a flush under XP Pro SP2 and it still had the current
time for last access. You are right, it's a catch-22.

.. Ed

Quote:
Mark Jacobs wrote in message
news:4614eacb (AT) newsgroups (DOT) borland.com...

No, he means that the CloseHandle call sets the last access time to when
the CloseHandle happened! I don't see a way round this! Certainly, the
other 2 work fine (last modified and last created), but last access
includes the access required to set the last access date and time - a bit
of a Catch 22 situation!
Back to top
JF Jolin
Guest





PostPosted: Thu Apr 05, 2007 7:19 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

Hi Mark

I'm using BCB 5.5.1

Some programs can modify that date.
http://msdn2.microsoft.com/en-us/library/ms724933.aspx


For example with Microsoft it is possible to set this parameter to -1 like

SetFileTime( hFile, &fTimeCreate, &fTimeLastAccess, &fTimeLastWrite );

memset(&fTimeLastWrite, 0xFF, sizeof(FILETIME) );
SetFileTime( hFile, &fTimeCreate, &fTimeLastAccess, &fTimeLastWrite );
CloseHandle( hFile );

So the CloseHandle instruction doesn't alter that time.


Another alternative is to modify directly the file system.
But I don't know how ?


__
JF Jolin
Back to top
Ed Mulroy
Guest





PostPosted: Thu Apr 05, 2007 7:21 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

Good catch - one more of Microsoft's API "secret codes".

.. Ed

Quote:
JF Jolin wrote in message
news:op.tqbd12qajr09w3@localhost...

Hi Mark

I'm using BCB 5.5.1

Some programs can modify that date.
http://msdn2.microsoft.com/en-us/library/ms724933.aspx


For example with Microsoft it is possible to set this parameter to -1 like

SetFileTime( hFile, &fTimeCreate, &fTimeLastAccess, &fTimeLastWrite );

memset(&fTimeLastWrite, 0xFF, sizeof(FILETIME) );
SetFileTime( hFile, &fTimeCreate, &fTimeLastAccess, &fTimeLastWrite );
CloseHandle( hFile );

So the CloseHandle instruction doesn't alter that time.


Another alternative is to modify directly the file system.
But I don't know how ?
Back to top
Koen
Guest





PostPosted: Thu Apr 05, 2007 9:27 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

Mark Jacobs wrote:

Quote:

No, he means that the CloseHandle call sets the last access time to
when the CloseHandle happened! I don't see a way round this!
Certainly, the other 2 work fine (last modified and last created), but
last access includes the access required to set the last access date
and time - a bit of a Catch 22 situation!

No it doesn't. I have tested this before posting. I adjusted the last
access time, closed the handle, and then looked into the properties of the
file using MS Windows Explorer. The accesstime was adjusted correctly. Note
however that when you do this, the accesstime is adjusted again to now.
Back to top
JF Jolin
Guest





PostPosted: Thu Apr 05, 2007 10:10 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

Koen <koen (AT) ha (DOT) com> wrote:

Quote:
No it doesn't. I have tested this before posting. I adjusted the last
access time, closed the handle, and then looked into the properties of the
file using MS Windows Explorer. The accesstime was adjusted correctly. Note
however that when you do this, the accesstime is adjusted again to now.

I'm using BCB 5.5.1

With that version:

TimeCreate and TimeLastWrite are adjusted.
TimeLastAccess is not.


__
JF Jolin
Back to top
Koen
Guest





PostPosted: Fri Apr 06, 2007 8:10 am    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

JF Jolin wrote:

Quote:

TimeCreate and TimeLastWrite are adjusted.
TimeLastAccess is not.



I only adjusted the last access time, and set the TimeCreate and
TimeLastWrite parameters to 0. If you change these timestamps, it doesn't
sound weird to me that the last access time is changed again to the current
date.

Koen
Back to top
JF Jolin
Guest





PostPosted: Sat Apr 07, 2007 8:10 am    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

Hi Koen

Can you show me your code to ajust the parameter
*lpLastAccessTime of the instruction ::SetFileTime
to a date in the future or in the past but not today ?

May be I'll understand ...


Best regards

__
JF Jolin
Back to top
Darko Miletic
Guest





PostPosted: Sun Apr 08, 2007 5:15 am    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

JF Jolin wrote:
Quote:
Here is the scenario:

hFile = CreateFile( lpszFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );

SetFileTime( hFile, &fTimeCreate, &fTimeLastAccess, &fTimeLastWrite );

CloseHandle( hFile );


Whatever time is specified in fTimeLastAccess, that will be overwritten when CloseHandle instruction will be executed.

MSDN that states : "lpLastAccessTime
[in] A pointer to a FILETIME structure that contains the new date and
time the file was last accessed. The last access time includes the last
time the file was written to, read from, or (in the case of executable
files) run. This parameter can be NULL if the application does not need
to set this information."
And now the crucial line:
"To preserve the existing last access time for a file even after
accessing a file, call SetFileTime with this parameter set to -1 before
closing the file handle."

Here is the working sample:

#include <windows.h>
#include <string>
#include <iostream>
#include <ctime>

//Important kb about converting time
//http://support.microsoft.com/kb/167296

void UnixTimeToFileTime(std::time_t t, LPFILETIME pft)
{
LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000i64;
pft->dwLowDateTime = (DWORD)ll;
pft->dwHighDateTime = ll >> 32;
}

void changeFileTime(const std::string &fname) {

HANDLE hFile = CreateFile( fname.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (hFile != INVALID_HANDLE_VALUE){
FILETIME ft = {0UL}, ftminus = {-1L,-1L};
std::tm tmp = {0,0,14,7,3,2007 - 1900,0,0,0};
std::time_t timeValue = std::mktime(&tmp);
UnixTimeToFileTime(timeValue,&ft);
BOOL ret = SetFileTime(hFile,NULL,&ft,NULL);
ret = ret && SetFileTime(hFile, NULL, &ftminus, NULL);
if (ret == FALSE) {
std::cout << "could not change last access field." << std::endl;
}
CloseHandle(hFile);
}

}

int main () {
changeFileTime("<some existing file here>");
return 0;
}
Back to top
JF Jolin
Guest





PostPosted: Sun Apr 08, 2007 11:18 pm    Post subject: Re: How to effectively modify the last access time of a file Reply with quote

Hi Darko

I knew about the -1 parameter but made a wrong
manipulation which leads me to false results.

Thanks also for UNIX time notice conversion.


Good day

__
JF Jolin



07 Apr 2007 20:15:30 -0400, Darko Miletic <kiklop (AT) fibertel (DOT) com.ar> wrote:

Quote:
"To preserve the existing last access time for a file even after
accessing a file, call SetFileTime with this parameter set to -1 before
closing the file handle."
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.