user440297
user440297

Reputation: 1181

setLastModified date with Qt

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).

Upvotes: 5

Views: 6089

Answers (3)

Vishal Kumar
Vishal Kumar

Reputation: 41

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");
}

Upvotes: 4

puetzk
puetzk

Reputation: 10814

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.

Upvotes: 5

Martin Beckett
Martin Beckett

Reputation: 96109

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.

Upvotes: 1

Related Questions