在Qt中修改文件的lastModified/lastAccessed/created日期
Modify file's lastModified/lastAccessed/created date in Qt
我有一个复制一些文件的应用程序,但在复制过程中,上次修改等日期更改为当前日期和时间。在几个论坛上,我看到建议使用SETFILETIME call from the fileapi.h
as specified here on MS docs。
但是,快速搜索显示这些功能在此处可用 QFileDevice where one can use the setFileTime function with the following FileTime enum. Note, QFile or QFileInfo 不允许修改这些日期和时间,只能读取这些日期和时间。
QFileDevice::FileAccessTime 0 When the file was most recently accessed
(e.g. read or written to). QFileDevice::FileBirthTime 1 When the file
was created (may not be not supported on UNIX).
QFileDevice::FileMetadataChangeTime 2 When the file's metadata was
last changed. QFileDevice::FileModificationTime 3 When the file was
most recently modified.
如何从 QFileInfo
对象甚至 QFile
对象(在本例中,QFile
继承自 QFileDevice
- 例如 static_cast<>() 会有帮助吗?
或者,我是否应该使用 Windows FileAPI.h
- 我如何从某个文件位置和 QDateTime
修改 created
或 lastModified
时间?
我必须承认,由于 OP 的问题,我第一次注意到 QFile::setFileTime()。 (我从来没有必要调整我 S/W 编写的任何文件的时间戳。)出于好奇,我试图弄清楚这个问题。
这就是我得到的:
// Qt header:
#include <QtCore>
// main application
int main(int argc, char **argv)
{
auto showTimeStamps = [](const QFile &file) {
qDebug() << "File time stamps of" << file.fileName();
QFileInfo fileInfo(file);
qDebug() << "birthTime() :" << fileInfo.birthTime();
qDebug() << "lastModified():" << fileInfo.lastModified();
qDebug() << "lastAccessed():" << fileInfo.lastRead();
};
qDebug() << "Qt Version:" << QT_VERSION_STR;
{ QFile file("test.txt");
// create test file
qDebug() << "Write test.txt...";
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Failed to create test.txt!";
return 1;
}
if (file.write("Test.\n") < 0) {
qDebug() << "Failed to write to test.txt!";
return 1;
}
file.close();
qDebug() << "Done.";
// check file time stamps initially
showTimeStamps(file);
// manipulate file time stamps
const QDateTime timeStamp(QDate(1970, 5, 1), QTime(6, 0));
qDebug() << "Modify time stamp of creation of test.txt:";
if (!file.open(QIODevice::Append | QIODevice::Text)) {
qDebug() << "Failed to open test.txt!";
return 1;
}
if (!file.setFileTime(timeStamp, QFileDevice::FileBirthTime)) {
qDebug() << "Failed to modify create time!";
return 1;
}
file.close();
showTimeStamps(file);
qDebug() << "Modify time stamp of last modification of test.txt:";
if (!file.open(QIODevice::Append | QIODevice::Text)) {
qDebug() << "Failed to open test.txt!";
return 1;
}
if (!file.setFileTime(timeStamp, QFileDevice::FileModificationTime)) {
qDebug() << "Failed to modify last modified time!";
return 1;
}
file.close();
showTimeStamps(file);
qDebug() << "Modify time stamp of last access of test.txt:";
file.setFileTime(timeStamp, QFileDevice::FileAccessTime);
if (!file.open(QIODevice::Append | QIODevice::Text)) {
qDebug() << "Failed to open test.txt!";
return 1;
}
if (!file.setFileTime(timeStamp, QFileDevice::FileAccessTime)) {
qDebug() << "Failed to modify last access time!";
return 1;
}
file.close();
showTimeStamps(file);
}
// bail out to enable check in File Explorer afterwards
return 0;
// delete test file
{ qDebug() << "Delete test.txt...";
if (!QFile::remove("test.txt")) {
qDebug() << "Failed to delete test.txt!";
return 1;
}
qDebug() << "Done.";
}
}
输出:
Qt Version: 5.13.0
Write test.txt...
Done.
File time stamps of "test.txt"
birthTime() : QDateTime(2020-06-18 08:42:09.983 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastModified(): QDateTime(2020-06-18 08:42:09.984 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastAccessed(): QDateTime(2020-06-18 08:42:09.983 Mitteleuropõische Sommerzeit Qt::LocalTime)
Modify time stamp of creation of test.txt:
File time stamps of "test.txt"
birthTime() : QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastModified(): QDateTime(2020-06-18 08:42:09.984 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastAccessed(): QDateTime(2020-06-18 08:42:09.983 Mitteleuropõische Sommerzeit Qt::LocalTime)
Modify time stamp of last modification of test.txt:
File time stamps of "test.txt"
birthTime() : QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastModified(): QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastAccessed(): QDateTime(2020-06-18 08:42:09.983 Mitteleuropõische Sommerzeit Qt::LocalTime)
Modify time stamp of last access of test.txt:
File time stamps of "test.txt"
birthTime() : QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastModified(): QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastAccessed(): QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
之后我在 Windows 文件资源管理器中验证了时间戳:
所以,我想知道 OP
的声明
Note, QFile or QFileInfo does not allow modifying these dates & times, only reading these dates & times.
我曾预料到这就是 QFile::setFileTime() 的实际意图,而且它似乎在我这边正常工作。
我有一个复制一些文件的应用程序,但在复制过程中,上次修改等日期更改为当前日期和时间。在几个论坛上,我看到建议使用SETFILETIME call from the fileapi.h
as specified here on MS docs。
但是,快速搜索显示这些功能在此处可用 QFileDevice where one can use the setFileTime function with the following FileTime enum. Note, QFile or QFileInfo 不允许修改这些日期和时间,只能读取这些日期和时间。
QFileDevice::FileAccessTime 0 When the file was most recently accessed (e.g. read or written to). QFileDevice::FileBirthTime 1 When the file was created (may not be not supported on UNIX). QFileDevice::FileMetadataChangeTime 2 When the file's metadata was last changed. QFileDevice::FileModificationTime 3 When the file was most recently modified.
如何从 QFileInfo
对象甚至 QFile
对象(在本例中,QFile
继承自 QFileDevice
- 例如 static_cast<>() 会有帮助吗?
或者,我是否应该使用 Windows FileAPI.h
- 我如何从某个文件位置和 QDateTime
修改 created
或 lastModified
时间?
我必须承认,由于 OP 的问题,我第一次注意到 QFile::setFileTime()。 (我从来没有必要调整我 S/W 编写的任何文件的时间戳。)出于好奇,我试图弄清楚这个问题。
这就是我得到的:
// Qt header:
#include <QtCore>
// main application
int main(int argc, char **argv)
{
auto showTimeStamps = [](const QFile &file) {
qDebug() << "File time stamps of" << file.fileName();
QFileInfo fileInfo(file);
qDebug() << "birthTime() :" << fileInfo.birthTime();
qDebug() << "lastModified():" << fileInfo.lastModified();
qDebug() << "lastAccessed():" << fileInfo.lastRead();
};
qDebug() << "Qt Version:" << QT_VERSION_STR;
{ QFile file("test.txt");
// create test file
qDebug() << "Write test.txt...";
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Failed to create test.txt!";
return 1;
}
if (file.write("Test.\n") < 0) {
qDebug() << "Failed to write to test.txt!";
return 1;
}
file.close();
qDebug() << "Done.";
// check file time stamps initially
showTimeStamps(file);
// manipulate file time stamps
const QDateTime timeStamp(QDate(1970, 5, 1), QTime(6, 0));
qDebug() << "Modify time stamp of creation of test.txt:";
if (!file.open(QIODevice::Append | QIODevice::Text)) {
qDebug() << "Failed to open test.txt!";
return 1;
}
if (!file.setFileTime(timeStamp, QFileDevice::FileBirthTime)) {
qDebug() << "Failed to modify create time!";
return 1;
}
file.close();
showTimeStamps(file);
qDebug() << "Modify time stamp of last modification of test.txt:";
if (!file.open(QIODevice::Append | QIODevice::Text)) {
qDebug() << "Failed to open test.txt!";
return 1;
}
if (!file.setFileTime(timeStamp, QFileDevice::FileModificationTime)) {
qDebug() << "Failed to modify last modified time!";
return 1;
}
file.close();
showTimeStamps(file);
qDebug() << "Modify time stamp of last access of test.txt:";
file.setFileTime(timeStamp, QFileDevice::FileAccessTime);
if (!file.open(QIODevice::Append | QIODevice::Text)) {
qDebug() << "Failed to open test.txt!";
return 1;
}
if (!file.setFileTime(timeStamp, QFileDevice::FileAccessTime)) {
qDebug() << "Failed to modify last access time!";
return 1;
}
file.close();
showTimeStamps(file);
}
// bail out to enable check in File Explorer afterwards
return 0;
// delete test file
{ qDebug() << "Delete test.txt...";
if (!QFile::remove("test.txt")) {
qDebug() << "Failed to delete test.txt!";
return 1;
}
qDebug() << "Done.";
}
}
输出:
Qt Version: 5.13.0
Write test.txt...
Done.
File time stamps of "test.txt"
birthTime() : QDateTime(2020-06-18 08:42:09.983 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastModified(): QDateTime(2020-06-18 08:42:09.984 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastAccessed(): QDateTime(2020-06-18 08:42:09.983 Mitteleuropõische Sommerzeit Qt::LocalTime)
Modify time stamp of creation of test.txt:
File time stamps of "test.txt"
birthTime() : QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastModified(): QDateTime(2020-06-18 08:42:09.984 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastAccessed(): QDateTime(2020-06-18 08:42:09.983 Mitteleuropõische Sommerzeit Qt::LocalTime)
Modify time stamp of last modification of test.txt:
File time stamps of "test.txt"
birthTime() : QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastModified(): QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastAccessed(): QDateTime(2020-06-18 08:42:09.983 Mitteleuropõische Sommerzeit Qt::LocalTime)
Modify time stamp of last access of test.txt:
File time stamps of "test.txt"
birthTime() : QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastModified(): QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
lastAccessed(): QDateTime(1970-05-01 06:00:00.000 Mitteleuropõische Sommerzeit Qt::LocalTime)
之后我在 Windows 文件资源管理器中验证了时间戳:
所以,我想知道 OP
的声明Note, QFile or QFileInfo does not allow modifying these dates & times, only reading these dates & times.
我曾预料到这就是 QFile::setFileTime() 的实际意图,而且它似乎在我这边正常工作。