遍历目录

删除

  1. /*
  2. * 删除文件夹
  3. */
  4. static bool deleteDirectory(const QString &path)
  5. {
  6. if (path.isEmpty())
  7. return false;
  8. QDir dir(path);
  9. if(!dir.exists())
  10. return true;
  11. dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
  12. QFileInfoList fileList = dir.entryInfoList();
  13. foreach (QFileInfo fi, fileList)
  14. {
  15. if (fi.isFile())
  16. fi.dir().remove(fi.fileName());
  17. else
  18. deleteDirectory(fi.absoluteFilePath());
  19. }
  20. return dir.rmpath(dir.absolutePath());
  21. }

拷贝

  1. /*
  2. * 拷贝文件夹
  3. */
  4. static bool copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
  5. {
  6. QDir sourceDir(fromDir);
  7. QDir targetDir(toDir);
  8. if(!targetDir.exists())
  9. { /* 如果目标目录不存在,则进行创建 */
  10. if(!targetDir.mkdir(targetDir.absolutePath()))
  11. return false;
  12. }
  13. QFileInfoList fileInfoList = sourceDir.entryInfoList();
  14. foreach(QFileInfo fileInfo, fileInfoList){ /* 遍历源文件夹内所有文件 */
  15. if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
  16. continue;
  17. if(fileInfo.isDir()){ /* 当为目录时,递归的进行copy */
  18. if(!copyDirectoryFiles(fileInfo.filePath(),
  19. targetDir.filePath(fileInfo.fileName()),
  20. coverFileIfExist))
  21. return false;
  22. }
  23. else{ /* 当允许覆盖操作时,将旧文件进行删除操作 */
  24. if(coverFileIfExist && targetDir.exists(fileInfo.fileName())){
  25. targetDir.remove(fileInfo.fileName());
  26. }
  27. // 进行文件copy
  28. if(!QFile::copy(fileInfo.filePath(),
  29. targetDir.filePath(fileInfo.fileName()))){
  30. return false;
  31. }
  32. }
  33. }
  34. return true;
  35. }

获得当前路径

  1. // 获取当前进程的全路径
  2. qApp->applicationFilePath();
  3. // 获得当前程序, 所在目录
  4. qDebug()<< "current applicationDirPath: " << QCoreApplication::applicationDirPath();
  5. // 获得当前工作路径
  6. // 一般启动时, 为程序所在目录的上层
  7. qDebug()<< "current currentPath: " << QDir::currentPath();

拼接路径