Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there a way to detect when a file copy process finish?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 9.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    paganotti
    wrote on last edited by
    #1

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

    thanks,
    Nicola

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      This probably depends how you are copying the file.
      If you use "QFile::copy":http://doc.qt.nokia.com/4.7/qfile.html#copy-2 it should tell you at the end of copy process, if successful or not. So, if it returns the copying shall have finished.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jupiter
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          paganotti
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • P Offline
              P Offline
              paganotti
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                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":https://bugreports.qt.nokia.com/ 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.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  KA51O
                  wrote on last edited by
                  #8

                  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(m_fileInfoList.at(m_positionInFileInfoList).lastModified() < QDateTime::currentDateTime().addSecs(-1))
                  {
                  readFile();
                  }
                  else
                  {
                  if(m_iLoopCount < 360)
                  {
                  QTimer::singleShot(500, this, SLOT(tryToReadFileLoop()));
                  m_iLoopCount += 1;
                  }
                  else
                  {
                  //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.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KA51O
                    wrote on last edited by
                    #9

                    Here's an old link on the problem: "How to test if a file is already open":http://lists.trolltech.com/qt-interest/2006-01/msg00251.html

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved