September 26, 2011

paganotti paganotti
Lab Rat
13 posts

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?

thanks,
Nicola

8 replies

September 26, 2011

koahnig koahnig
Gene Splicer
2905 posts

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

September 26, 2011

Jupiter Jupiter
Lab Rat
95 posts

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

September 26, 2011

paganotti paganotti
Lab Rat
13 posts

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.

September 26, 2011

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

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

September 26, 2011

paganotti paganotti
Lab Rat
13 posts

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?

September 26, 2011

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

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

September 27, 2011

KA51O KA51O
Hobby Entomologist
478 posts

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.

  1. void FileReader::tryToReadFileLoop()
  2. {
  3.   if(m_fileInfoList.at(m_positionInFileInfoList).lastModified() < QDateTime::currentDateTime().addSecs(-1))
  4.   {
  5.     readFile();
  6.   }
  7.   else
  8.   {
  9.     if(m_iLoopCount < 360)
  10.     {
  11.        QTimer::singleShot(500, this, SLOT(tryToReadFileLoop()));
  12.        m_iLoopCount += 1;
  13.     }
  14.     else
  15.     {
  16.        //something went wrong file exists but could not be read
  17.        emit fileCouldNotBeRead(m_sFilePath);
  18.     }
  19.   }
  20. }

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.

September 27, 2011

KA51O KA51O
Hobby Entomologist
478 posts

Here’s an old link on the problem: How to test if a file is already open [lists.trolltech.com]

 
  ‹‹ [SOLVED]problem with resources: file not found      [SOLVED] Creating Instance of dialog error ››

You must log in to post a reply. Not a member yet? Register here!