Home:ALL Converter>Avoid updating last-accessed date/time when reading a file

Avoid updating last-accessed date/time when reading a file

Ask Time:2011-04-14T21:11:08         Author:Anodyne

Json Formatter

We're building a Windows-based application that traverses a directory structure recursively, looking for files that meet certain criteria and then doing some processing on them. In order to decide whether or not to process a particular file, we have to open that file and read some of its contents.

This approach seems great in principle, but some customers testing an early version of the application have reported that it's changing the last-accessed time of large numbers of their files (not surprisingly, as it is in fact accessing the files). This is a problem for these customers because they have archive policies based on the last-accessed times of files (e.g. they archive files that have not been accessed in the past 12 months). Because our application is scheduled to run more frequently than the archive "window", we're effectively preventing any of these files from ever being archived.

We tried adding some code to save each file's last-accessed time before reading it, then write it back afterwards (hideous, I know) but that caused problems for another customer who was doing incremental backups based on a file system transaction log. Our explicit setting of the last-accessed time on files was causing those files to be included in every incremental backup, even though they hadn't actually changed.

So here's the question: is there any way whatsoever in a Windows environment that we can read a file without the last-accessed time being updated?

Thanks in advance!

EDIT: Despite the "ntfs" tag, we actually can't rely on the filesystem being NTFS. Many of our customers run our application over a network, so it could be just about anything on the other end.

Author:Anodyne,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5663766/avoid-updating-last-accessed-date-time-when-reading-a-file
Luke :

The documentation indicates you can do this, though I've never tried it myself.\n\n\n To preserve the existing last access time for a file even after accessing a file, call SetFileTime immediately after opening the file handle with this parameter's FILETIME structure members initialized to 0xFFFFFFFF.\n",
2011-04-14T13:26:04
Suresh :

From Vista onwards NTFS does not update the last access time by default. To enable this see http://technet.microsoft.com/en-us/library/cc959914.aspx \n\nStarting NTFS transaction and rolling back is very bad, and the performance will be terrible. \n\nYou can also do\n\nFSUTIL behavior set disablelastaccess 0",
2011-04-27T01:58:48
yy