How to obtain file metadata??

I am trying to write a function that detects if a file has been modified since its last input operation. Rather than just re-reading the file which can be computationally expensive especially for large files, I would like to use the file's metadata Created, Modified, and Accessed that you can see just by right clicking the file and viewing properties and in the general tab. How can I read a files metadata with C++? Thanks!
Last edited on
@ OP: This is a pretty popular need so there are a few ways of going about this depending on what else you are doing at the same time.

1: Use "CreateFile()" with 'dwCreationDisposition' set to OPEN_EXISTING to get the files handle (unless you already have it obviously) and feed it to the "GetFileInformationByHandle()" function to fill in 'BY_HANDLE_FILE_INFORMATION' struct. The property you want is 'ftLastWriteTime'.

2: Use "CreateFile()" with 'dwCreationDisposition' set to OPEN_EXISTING to get the files handle and feed it to "GetFileTime()" to populate a 'FILETIME' struct which then has to be entered into "FileTimeToSystemTime()" to be useful.

2a: Instead of using "FileTimeToSystemTime()", try saving the most previous instance of the 'FILETIME' struct where a change has taken place in another variable and use "CompareFileTime()" when you check so that you're not playing around with comparing the structs yourself.

3: If you need to enumerate the files in a directory as well then use "FindFirstFile()" & "FindNextFile()" and the data you're looking for is part of the 'WIN32_FIND_DATA' struct.

- CreateFile():

- GetFileInformationByHandle():


- GetFileTime():


- FileTimeToSystemTime():

- CompareFileTime():

EDIT: Also in case you want a full example to compare against:

EDIT 2: An additional concern to keep in mind is that updating this data is a setting in Windows. The command line app "FSUTIL" allows you to disable this feature as well as the registry keys 'HKEY_LOCAL_MACHINE\SYSTEM \CurrentControlSet\Control\FileSystem\NtfsDisableLastAccessUpdate'. But by default these things are not done so it probably won't matter.
Last edited on
Topic archived. No new replies allowed.