Results 1 to 2 of 2

Thread: File timestamp transfer on windows in QT when copying files

  1. #1
    Join Date
    Jun 2010
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default File timestamp transfer on windows in QT when copying files

    Since QT doesn't nativelly copy over file information when it does a file copy (specifically creation, last modified and last accessed times), and I needed this, I've written a function that does this using a combination of QT and the Windows API.

    I thought someone else might find it useful, because I found a fair few posts on the net with people asking to do this very thing. It's not cross platform, obviously.
    I may add #ifdef stuff and put in linux attribute copying as well later, when I need it.

    Qt Code:
    1. #include <QObject>
    2. #include <windows.h>
    3. #include <qfile.h>
    4. #include <qtextstream.h>
    5. #include <iostream>
    6. #include <qdir>
    7. #include <QString>
    8. // file copy operation that preserves file timestamps
    9. bool copyFile(QString srcFile,QString dstFile) {
    10. QFile sourceFile(srcFile);
    11. FILETIME ftCreate, ftAccess, ftWrite;
    12. if (!sourceFile.open(QIODevice::ReadOnly))
    13. return false;
    14. QFile destFile(dstFile);
    15. if (!destFile.open(QIODevice::WriteOnly))
    16. return false;
    17. destFile.write(sourceFile.readAll());
    18. sourceFile.close();
    19. destFile.close();
    20. wchar_t *srcPath = new wchar_t[srcFile.length()];
    21. for (int i(0);i<srcFile.length();i++) {
    22. srcPath[i] = srcFile.toStdString().c_str()[i];
    23. }
    24. wchar_t *dstPath = new wchar_t[dstFile.length()];
    25. for (int x(0);x<dstFile.length();x++) {
    26. dstPath[x] = dstFile.toStdString().c_str()[x];
    27. }
    28. HANDLE sfile = CreateFile(srcPath,0,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_READONLY,NULL);
    29. HANDLE dfile = CreateFile(dstPath,GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    30. GetFileTime(sfile,&ftCreate, &ftAccess, &ftWrite);
    31. SetFileTime(dfile,&ftCreate, &ftAccess, &ftWrite);
    32. CloseHandle(sfile);
    33. CloseHandle(dfile);
    34. delete [] srcPath;
    35. delete [] dstPath;
    36. return true;
    37. }
    To copy to clipboard, switch view to plain text mode 

    I've taken this out of a larger class that does a lot more file and directory operations, so some of the includes here might not be needed. This code is also on my blog, but since I'm still a relativelly new poster here I thought links to personal sites might not be allowed.
    Last edited by rucs_hack; 28th April 2011 at 13:10.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: File timestamp transfer on windows in QT when copying files

    Since you are being Windows specific, you could also just use CopyFile(from,to) function which also copies file attributes (eg. read only).

    If you want to copy security attributes also, you can use SHFileOperation with wFunc set to FO_COPY

Similar Threads

  1. Transfer files on WLAN using QT
    By bilalsaeed in forum Qt Programming
    Replies: 1
    Last Post: 3rd January 2011, 13:29
  2. problem copying files and unmount drives
    By alejo in forum Qt Programming
    Replies: 2
    Last Post: 14th July 2009, 20:54
  3. Only copying files using qmake
    By sagacity in forum Installation and Deployment
    Replies: 2
    Last Post: 26th June 2009, 14:03
  4. Replies: 2
    Last Post: 2nd April 2008, 17:28
  5. Replies: 2
    Last Post: 5th September 2007, 22:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.