Is there a way to detect when a file copy process finish?
-
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. -
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.
-
-
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.
-
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.
-
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