954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to set file access time

Here the code where I'm trying to set last-accessed attribute of the file to some value.

hFile = CreateFile(szDestName.GetBuffer(255), GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);

GetFileTime(hFile,&createTime,&accessTime,&writeTime);
writeTime.dwHighDateTime -= 1000L;
accessTime = writeTime;

SetFileTime(hFile,&createTime,&accessTime,&writeTime);
// at this point access and write(modified) times are equal
CloseHandle(hFile);
// at this point access time is changed to the current system time


I suppose I didn't set the correct attributes in CreateFile function.
Does anybody knows how to do this correctly?

akoloff
Newbie Poster
17 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

you can't control the time because, as you found out, the os will change it when the file is closed.
Not all file systems can record creation and last access time and not all file systems
record them in the same manner. For example, on Windows NT FAT, create time has a
resolution of 10 milliseconds, write time has a resolution of 2 seconds, and access time
has a resolution of 1 day (really, the access date). On NTFS, access time has a
resolution of 1 hour. Therefore, the GetFileTime function may not return the same file
time information set using SetFileTime. Furthermore, FAT records times on disk in local
time. However, NTFS records times on disk in UTC, so it is not affected by changes in
time zone or daylight saving time.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I found out that if you use:

CreateFile(szDestName.GetBuffer(255), 0, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);

instead of:
CreateFile(szDestName.GetBuffer(255), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);

then you can't read or write to the file obviously BUT:
you can check for the file existence without changing
it's access time, which was good enough in my case.

Thanks for your reply anyway.

akoloff
Newbie Poster
17 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

If all you wanted to do what check for the file's existance then use GetFileAttributes(). That function returns -1 if the file doesn't exist. Or you could also use _stat() function, which also returns error when file not found.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You