5

I need to set the last modified date for some files. It is an essential ability for my application.

I don't see how I can do this with QT. I don't see a method/function for it. I can read the dates with Qt but I don't see how I can set the dates.

Now... I know some (I think all actually) windows API's from MS have this ability.

The thing is that I am not looking for a "Windows" only solution. I need this ability to work on Windows, Linux, and Mac. This is why I choose Qt...

Any solution proposed should be API centric (don't want a system command/utility) and the solution should cover the 3 main OS's (Win,Linux,Mac).

1
  • Did you find a solution? I have same problem. Is it possible using Qt?. Thanks Aug 24, 2016 at 20:55

3 Answers 3

5

The posix function for this is utime(2), which is also available on windows as part of MSVCRT (see MSDN). So using that should cover Mac, Linux, and Windows.

Since QAbstractFileEngine doesn't expose this functionality, I don't think it'll be available in any other Qt filesystem APIs either.

4

In windows include qt_windows.h and play with modified and creation date.

QDateTime newModifiedTime=ui->dateTimeEdit_modified->dateTime();
QDateTime newCreationDate=TimeEdit_Creation->dateTime();
FILETIME pmodifiedtime,pcreationtime;
    HANDLE hfile;
    LPCWSTR filename=Filename.toStdWString().c_str();
          hfile=CreateFile(filename,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hfile==INVALID_HANDLE_VALUE)
    {
        QMessageBox::information(this,"Failed","Unable to open file,check file permission");
    }
    else
    {
        LONGLONG ll = Int32x32To64(newCreationDate.toTime_t(), 10000000) + 116444736000000000;
        pcreationtime.dwLowDateTime = (DWORD) ll;
        pcreationtime.dwHighDateTime = ll >> 32;
        LONGLONG ll2 = Int32x32To64(newModifiedTime.toTime_t(), 10000000) + 116444736000000000;
        pmodifiedtime.dwLowDateTime = (DWORD) ll2;
        pmodifiedtime.dwHighDateTime = ll2 >> 32;

        if(!SetFileTime(hfile,&pcreationtime,NULL,&pmodifiedtime))
        {
           QMessageBox::information(this,"Failed","Unable to set Date And Time");
        }
        else
        {
           QMessageBox::information(this,"Success","Date And Time Updated");
           ui->lineEdit_FileName->clear();
        }
    }
    CloseHandle(hfile);

For linux add include utime.h and fcntl.h then

struct utimbuf timebuffer;
timebuffer.modtime=newModifiedTime.toTime_t();
const char *filename=Filename.toAscii();
if((utime(filename,&timebuffer))<0)
{
        QMessageBox::information(this,"Error","Unable to set Time");
}
else
{
        QMessageBox::information(this,"Success","Date Changed");
}
1
  • 3
    LPCWSTR filename=Filename.toStdWString().c_str(); is a very bad advice, because filename will store the pointer to deleted temporary object
    – borisbn
    Feb 5, 2016 at 13:29
1

I don't think there is anything specific. Easiest way is probably to open the file for write+append and the close it again - this lets the OS/Filesystem update the modified time.

You could also try reading the permissions and calling setPerimssions, that's sometimes used as a safer way of updating a file since it's non-blocking.

edit: to set an arbitrary last modified time you could use - on windows SetFileTime function.

There is a boost::filesystem::last_write_time( ) you migth want to look at - I haven't used it.

5
  • That would not work at all. If dateTime now is B, I copy a file that was last modified at dateTime A, the copy will have modified dateTime of B. Down the road, as time passes, dateTime will be C. If at that time I open and save the file as you suggest it will have a last modified dateTime of C. Obviously this is pointless as the lastModified dataTime will be even more "off" because I need it to be A and not C. I need the lastModified dateTime to be the same as the original file.
    – user440297
    Feb 11, 2011 at 2:20
  • Sorry ? - you set the last modified time to 'now' and you do this by modifying the file 'now' ! Feb 11, 2011 at 2:35
  • I don't want to set the last modified time to 'now'. Hence, what you suggest will not work. All MS API's allow the setting of file time stamps as does Java and other mature API's. Qt can get time stamps but can't set time stamps as far as I know. If I have to I'll use the WinAPI but that leaves me in the dark with regards to Linux and MAC.
    – user440297
    Feb 11, 2011 at 2:56
  • 1
    I want to preserve the time stamps of my original file in the copy... specifically in regards to the last modification time stamp.
    – user440297
    Feb 11, 2011 at 3:20
  • I actually need the "now" option and I tried this. I'm working on Windows with Qt 5.6. Opening the file for write + append and closing it without actually writing anything did not update the last modified timestamp for me. So far, I haven't found a solution that's cross-platform using Qt's classes.
    – goug
    May 14, 2019 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.