简体   繁体   中英

C++ how to get the recently changed timestamp of a file?

I would like to determine the last recently changed timestamp of a file on Windows.

Yes, I have used the stat function with st_atime, st_mtime, st_ctime. However Windows is not updating these timestamps for the specific files. (I don't want to change this behaviour, because this will be client specific later on).

So, How can i determine the last recently changed timestamp of a file?

eg

  • Timestamp on renaming the file's name ( Can't get this to work so far )

  • Timestamp on modifiying the file (mctime provides this, but I would prefare a one way solution)

Thanks in advance.

If you are on windows, use GetFileTime ( https://msdn.microsoft.com/en-us/library/windows/desktop/ms724320(v=vs.85).aspx ) where you have opened your file via CreateFile ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx ).

I really recommend reading the article " Retrieving the Last-Write Time " for this purpose: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724926(v=vs.85).aspx

Edit after your comments:

In your app's include part:

#include <string>
#include <sstream>
#include <iostream>

in the method GetLastWriteTime from the link instead of the StringCchPrintf add:

// Build a string showing the date and time.
std::stringstream ss;
ss << stLocal.wYear << "-" << stLocal.wMonth << "-" << stLocal.wDay << " " << stLocal.wHour << ":" << stLocal.wMinute ;
std::string timeString = ss.str();
std::cout << timeString;

and please read the documentation from: http://www.cprogramming.com/tutorial/c++-iostreams.html and then the reference from http://en.cppreference.com/w/cpp/io/basic_stringstream

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM