Is there a way to detect when a file copy process finish?

  • Good Morning,
    Is there a function or library Qt that say me when a copy file process finish?


  • Moderators

    This probably depends how you are copying the file.
    If you use "QFile::copy": it should tell you at the end of copy process, if successful or not. So, if it returns the copying shall have finished.

  • you can call the QFile::copy method in a thread and emit a signal after the copy

  • I monitor a directory call "share" of file system with QFileSystemWatcher. I would that QFileSystemWatcher emit signal directoryChanged when the file copy process on directory "share" finished. It is possible? Now QFileSystemWatcher emit signal at start of file copy process and not at end.

  • Have you tried adding the newly created file to QFileSystemWatcher and connecting to the fileChanged() signal?

  • Yes, I obtain the same result. I copy large file to the directory share. fileChanged() signal is emitted when copy file process is begined, not at end of the copy process. I would that fileChanged signal is emitted at end of copy process. How can I do?

  • Well, that's a good question then.

    I assume QFileSystemWatcher uses the Windows API and so inherits its limitations. For example the Windows API is known to raise change events only when data is actually written to disk (data might reside in cache for a while).

    As a temporary workaround I would set up a QTimer which monitors file size and as soon as the size is stable for a certain amount of time I would assume the operation is completed. You could an add additional check by trying to open the file for writing (should fail if file is still beeing copied).

    You might raise a bugreport on "JIRA": and see what the Qt devs think about it. Keep in mind that QFileSystemWatcher is considered deprecated due to flawed design. You might see an improved version of it in the near to mid future.

  • I had the same problem. My solution was to periodically check when the file was last modified and if it has not been modified in the last second I start reading it.

    @void FileReader::tryToReadFileLoop()
    if( < QDateTime::currentDateTime().addSecs(-1))
    if(m_iLoopCount < 360)
    QTimer::singleShot(500, this, SLOT(tryToReadFileLoop()));
    m_iLoopCount += 1;
    //something went wrong file exists but could not be read
    emit fileCouldNotBeRead(m_sFilePath);

    an even better solution would be if you implement different versions for the different OS, and use the OS specific methods, like for example "fuser" or "lsof" (this is for Linux) to see if the file is currently opened by another process.

  • Here's an old link on the problem: "How to test if a file is already open":

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.