I've had a lot of success using Boost's filesystem::last_write_time(path)
, although my Windows experience tells me that if you're making a lot of writes to the file in a short timespan, then the resolution on the returned timestamp isn't enough to differentiate if you're asking for the timestamp after each write
You could use it like so:
boost::filesystem::path filePath = "Path/To/My/File.txt"
std::time_t writeTime = boost::filesystem::last_write_time(filePath);
std::ostringstream ss;
ss << std::put_time(&writeTime, "%c %Z");
std::string timeString = ss.str();
References:
Edit:
Since the OS doesn't update the file's timestamp on renamed, you will have to unfortunately start listening to events. This SO post has some good information on hooking into those from C++.
The C++ side has ReadDirectoryChangesW
, which allows you to pass a callback as a LPOVERLAPPED_COMPLETION_ROUTINE
Like a lot of OS-specific code, it can get pretty hard to follow pretty quickly.
As for "touching" the file on rename so that the timestamp is updated, you could copy it to itself. See CopyFile
If you're not against writing managed C++ code there's the FileSystemWatercher's Renamed Event