我想确定Windows上最近更改的文件时间戳。
是的,我已将stat函数与st_atime,st_mtime,st_ctime一起使用。但是,Windows不会更新特定文件的这些时间戳。 (我不想改变这种行为,因为稍后这将是客户端特定的。)
那么,我如何确定文件的最近更改时间戳?
e.g。
重命名文件名称的时间戳(目前无法使其正常工作)
修改文件的时间戳(mctime提供了这个,但我会提供单向解决方案)
提前致谢。
答案 0 :(得分:1)
我使用Boost的filesystem::last_write_time(path)
取得了很大的成功,虽然我的Windows经验告诉我,如果你在短时间内对文件进行大量写操作,那么返回时间戳的分辨率如果你在每次写入后要求时间戳
您可以像这样使用它:
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();
参考文献:
由于操作系统在重命名时不更新文件的时间戳,因此您不得不开始收听事件。 This SO post提供了一些关于从C ++中学习的信息。
C ++方面有ReadDirectoryChangesW
,它允许你将回调作为LPOVERLAPPED_COMPLETION_ROUTINE
传递。就像很多特定于操作系统的代码一样,很快就会很难跟上它。
至于“触摸”重命名文件以便更新时间戳,您可以将其复制到自身。见CopyFile
如果您不反对编写托管C ++代码,那就是FileSystemWatercher的Renamed Event
答案 1 :(得分:0)
如果您使用的是Windows,请使用GetFileTime
(https://msdn.microsoft.com/en-us/library/windows/desktop/ms724320(v=vs.85).aspx),通过CreateFile
(https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx)打开文件。
我真的建议您为此目的阅读文章“检索上次写入时间”:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724926(v=vs.85).aspx
评论后编辑:
在您的应用中包含部分:
#include <string>
#include <sstream>
#include <iostream>
在方法GetLastWriteTime
中,而不是StringCchPrintf
添加:
// 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;
请阅读以下文档:http://www.cprogramming.com/tutorial/c++-iostreams.html,然后阅读http://en.cppreference.com/w/cpp/io/basic_stringstream
中的参考文档