将复制文件的QDateTime调整为源文件的QDateTime

问题描述

上下文:

我有一个应用程序,可以搜索目录中的文件并复制特定文件

问题:

使用FilecopyExW,我已成功将文件复制到新位置,但是文件的日期和时间不匹配,可能会延迟一秒钟或更长时间,如下例所示。

尝试用源文件名称有时修改覆盖目标文件QDateTime不能正确写入日期,如下面的示例所示。

调试器的屏幕截图

enter image description here

文件的日志输出

09:34:50.731 Warning: File times do not match
09:34:50.731 Warning: Birth time differene:  0
09:34:50.731 Warning: Last Access time differene:  483
09:42:02.660 Warning: Last Modified time differene:  483
09:42:02.660 Debug: 

输出其他随机失败的文件

09:43:48.831 Warning: File times do not match
09:43:48.831 Warning: Birth time differene:  -7
09:43:48.831 Warning: Last Access time differene:  1462
09:44:53.602 Warning: Last Modified time differene:  0
09:44:53.602 Debug: 

有什么想法会发生这种情况以及如何解决


代码示例:

 // File copied successfully at this point,Now adjust file times as these may not be accurate (why?)

 QFile newFile(destination);
 if (newFile.open(qiodevice::OpenModeFlag::writeonly | qiodevice::OpenModeFlag::Append)) {

      // set file attributes,create,last write and last modified dates the same as original file
      QFileInfo srcFi(storageFile.location);

      // Set last modified date - most important
      QDateTime localLastModified = srcFi.lastModified().toLocalTime();
      if (!newFile.setFileTime(localLastModified,QFileDevice::FileModificationTime)) {
           printWarningFileError(TAG,QString("Failed to set lastModified date for file at [%1]").arg(destination),&newFile);
      }

      // Set last accessed date
      QDateTime localLastRead = srcFi.lastRead().toLocalTime();
      if (!newFile.setFileTime(localLastRead,QFileDevice::FileAccesstime)) {
           printWarningFileError(TAG,QString("Failed to set lastAccessed date for file at [%1]").arg(destination),&newFile);
      }

      // Set created date
      QDateTime localBirthTime = srcFi.birthTime().toLocalTime();
      if (!newFile.setFileTime(localBirthTime,QFileDevice::FileBirthTime)) {
           printWarningFileError(TAG,QString("Failed to set created date for file at [%1]").arg(destination),&newFile);
      }

      // Ensure we write out content before closing file (not really necessary due to close() )
      newFile.flush();
      newFile.close();

      // Read file & refresh Meta data for checking
      QFileInfo fiDest(destination);
      fiDest.refresh();

      // Read destination file date data
      QDateTime destFileBirth = fiDest.birthTime();
      QDateTime destFileAccess = fiDest.lastRead();
      QDateTime destFileModified = fiDest.lastModified();

      // Compare dates
      bool birthTimeMatch = (destFileBirth != localBirthTime);
      bool accesstimeMatch = (destFileAccess != localLastRead);
      bool modifiedTimeMatch = (destFileModified != localLastModified);

      if (!(birthTimeMatch && accesstimeMatch && modifiedTimeMatch)) {
           qWarning() << "File times do not match";
           qWarning() << "Birth time differene: " << (destFileBirth.toMSecsSinceEpoch() - localBirthTime.toMSecsSinceEpoch());
           qWarning() << "Last Access time differene: " << (destFileAccess.toMSecsSinceEpoch() - localLastRead.toMSecsSinceEpoch());
           qWarning() << "Last Modified time differene: " << (destFileModified.toMSecsSinceEpoch() - localLastModified.toMSecsSinceEpoch());
           qDebug() << "";
      }
      else {
           qDebug() << "All good";
      }
 }
 else {
      printWarningFileError(TAG,QString("Failed to open file for adjust dates [%1]").arg(destination),&newFile);
 }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...