Solved Set LastModified date of file
-
How can I change the last modified date in Windows? In Linux I changed it by
dateTimeEdit=new QDateTimeEdit(QDateTime::currentDateTime(),this);
dateTimeEdit->setDisplayFormat("yyyy.MMM.dd hh:mm");
exec=new QProcess(this)
exec->start("touch", QStringList() << "-t" <<dateTimeEdit->dateTime().toString("yyyyMMddhhmm")<<file_path);
exec->waitForFinished();Is there any platform independent way?
-
Hi
QFileInfo seems only to allow reading it
http://doc.qt.io/qt-5/qfileinfo.html#lastModifiedso utime could be used on win/linux
https://msdn.microsoft.com/en-us/library/4wacf567(v=vs.140).aspx
https://linux.die.net/man/2/utimeor if only win
https://msdn.microsoft.com/en-us/library/ms724933(VS.85).aspx -
Hi!
I think the following behaves pretty much liketouch
(changes the last modified date, creates the file if it didn't exist before):bool touch(const QString& filePath) { QFile file(filePath); if (!file.open(QIODevice::ReadWrite)) { return false; } const quint64 size = file.size(); file.seek(size); file.write( QByteArray(1, '0') ); file.resize(size); return true; }
-
@mrjj
I tried as following,#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/utime.h>
#include <time.h>struct tm tma = {0}, tmm = {0}; struct _utimbuf ut; tmm.tm_year = 102; tmm.tm_mon = 1; tmm.tm_mday = 15; tmm.tm_hour = 10; tmm.tm_min = 0; tmm.tm_sec = 0; ut.modtime = mktime(&tmm); // Show file time before and after if( _utime( "D:/kde/crt_utime.c", &ut ) == -1 ) perror( "_utime failed\n" ); else printf( "File time modified\n" ); system( "dir D:\kde" );
The result is
Volume in drive D has no label.
Volume Serial Number is EA68-9A51Directory of D:\kde
10/08/2016 02:51 PM <DIR> .
10/08/2016 02:51 PM <DIR> ..
02/15/2002 10:00 AM 0 crt_utime.c
1 File(s) 0 bytes
2 Dir(s) 151,142,400 bytes freeI can see the correctly chanaged time 10:00 AM. But when I check on my windows explorer, I can see the Date Modified is 9:00 AM. The time checked by system and explorer shows always 1 hour different result. I changed tmm.tm_isdst to positive, negative and zero, but still system and explorer show different time. Is my system infected by virus?
-
Hi
I dont think you have a virus :)You are right its related to the isdst / daylight savings.
http://stackoverflow.com/questions/3660983/c-time-t-problem
But it seems you tried all values for it and it still have the 1 hour diff so you should try some of the suggestion from the links.
Some seems to do
setenv("TZ", "", 1);
tzset();
before mktime to correct it. -
@samdol In fact, you may just pass a null pointer to the second argument of utime. Then it will be set to current time.
That is how I implement touch():
https://github.com/benlau/qtshell/blob/master/qtshell.cpp#L178
-
@benlau QtShell looks nice, thanks for sharing!
-
@benlau why you do this change? path.toLocal8Bit().constData()
-
@Jeronimo Convert QString to const char*.
p.s Just updated the source to use toUtf8() instead of toLocal8Bit().
-
In fact my computer adjust clock for Daylight saving time.
So I did,
Control Panel --> Time Zone Settings
--> Uncheck Automatically adjust clock for Daylight Saving Time.Now the lastmodified time on system and explorer are the same.
Thank you. -
@benlau
Hi Benla,
I tested the touch function in qtshell.cpp.bool QtShell::touch(const QString &path)
{
...
QByteArray bytes = path.toUtf8();if (utime(bytes.constData(), 0) == -1) { qWarning() << "utimes failed:" << path;
res = false;
...
}It works fine for english filename. But if the filename contains some chinese
charactors then it gives an error and I could not modified time stamp of file.
I am using Windows 7. Thank you. -
@samdol Hi, I just setup a Windows 10 to test. Both of VC and Mingw works. Could you send me the failed file name?
-
@benlau
Here is a zip file containing two files. link textThe following is the code, I tested on my Windows7 English version.
bool res = true; QFileInfo info(path); if (!info.exists()) { QFile file(path); if (!file.open(QIODevice::WriteOnly)) { qWarning() << "Failed to create file:" << path; res = false; } file.close(); } else { QByteArray bytes = path.toUtf8(); if (utime(bytes.constData(), 0) == -1) { qWarning() << "utimes failed:" << path; res = false; } }
-
Thank you for this, very simple, quick and it worked for me (tested only on windows so far)