Avoid updating date and time of last access when reading a file

We create a Windows-based application that recursively moves the directory structure, searches for files that match certain criteria, and then does some processing on them. To decide whether to process a particular file, we must open that file and read its contents.

This approach seems fundamental, but some clients testing the earlier version of the application reported that it changes the access time of a large number of their files (not surprising, since it actually accesses the files). This is a problem for these clients because they have archive policies based on files with the last access (for example, they archive files that were not available in the last 12 months). Since it is planned to launch our application more often than the โ€œwindowโ€ of the archive, we effectively prevent the archiving of any of these files.

We tried to add code to save every time I last accessed a file before reading it, and then writing it later (disgusting, I know), but this caused problems for another client that did incremental backups based on the file system transaction log, Our explicit setting of the time of the last access to files led to the fact that these files were included in each incremental backup, even if they did not actually change.

So, here is the question: is there any way in the Windows environment that we can read the file without updating the last time?

Thanks in advance!

EDIT: Despite the ntfs tag, we actually cannot rely on the file system, which is the NTFS file system. Many of our clients run our application over the network, so anything can be on the other end.

+4
source share
3 answers

The documentation indicates that you can do this, although I have never tried this myself.

To preserve the existing file access time even after accessing the file, call SetFileTime immediately after opening the file descriptor with this FILETIME structure element initialized to 0xFFFFFFFF.

+5
source

Starting with Vista, NTFS does not update the latest default access time. To enable this, see http://technet.microsoft.com/en-us/library/cc959914.aspx

Starting an NTFS transaction and rolling back is very bad, and performance will be terrible.

You can also do

FSUTIL disablelastaccess behavior statement 0

+3
source

I donโ€™t know what your clientโ€™s minimum requirements are, but have you tried NTFS transactions? On the desktop, the first OS to support was Vista, and on the server it was Windows Server 2008. But it might be worth a look.

Run the NTFS transaction, read the file, roll back the transaction. Just!:-). I really don't know if it will roll back the last access date. You will have to test it for yourself.

Here is a link to an MSDN Magazine article on NTFS transactions, which includes other links. http://msdn.microsoft.com/en-us/magazine/cc163388.aspx

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1348094/


All Articles