944,098 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6760
  • C RSS
Aug 20th, 2005
0

how to set file access time

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

  1. hFile = CreateFile(szDestName.GetBuffer(255), GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
  2.  
  3. GetFileTime(hFile,&createTime,&accessTime,&writeTime);
  4. writeTime.dwHighDateTime -= 1000L;
  5. accessTime = writeTime;
  6.  
  7. SetFileTime(hFile,&createTime,&accessTime,&writeTime);
  8. // at this point access and write(modified) times are equal
  9. CloseHandle(hFile);
  10. // 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?
Last edited by Dave Sinkula; Aug 20th, 2005 at 3:40 pm. Reason: Added [code][/code] tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akoloff is offline Offline
17 posts
since Jan 2005
Aug 20th, 2005
0

Re: how to set file access time

you can't control the time because, as you found out, the os will change it when the file is closed.
Quote originally posted by MSDN ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,960 posts
since Aug 2005
Aug 21st, 2005
0

Re: how to set file access time

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akoloff is offline Offline
17 posts
since Jan 2005
Aug 21st, 2005
0

Re: how to set file access time

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,960 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Pointers and Dynamic Arrays
Next Thread in C Forum Timeline: Days Since a Given Date





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC