zoukankan      html  css  js  c++  java
  • QT 删除文件指定目录

     1 bool deleteDir(const QString &dirName)
     2 {
     3     QDir directory(dirName);
     4     if (!directory.exists())
     5     {
     6         return true;
     7     }
     8 
     9 
    10     QString srcPath = QDir::toNativeSeparators(dirName);
    11     if (!srcPath.endsWith(QDir::separator()))
    12         srcPath += QDir::separator();
    13 
    14 
    15     QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
    16     bool error = false;
    17     for (QStringList::size_type i=0; i != fileNames.size(); ++i)
    18     {
    19         QString filePath = srcPath + fileNames.at(i);
    20         QFileInfo fileInfo(filePath);
    21         if (fileInfo.isFile() || fileInfo.isSymLink())
    22         {
    23             QFile::setPermissions(filePath, QFile::WriteOwner);
    24             if (!QFile::remove(filePath))
    25             {
    26                 qDebug() << "remove file" << filePath << " faild!";
    27                 error = true;
    28             }
    29         }
    30         else if (fileInfo.isDir())
    31         {
    32             if (!deleteDir(filePath))
    33             {
    34                 error = true;
    35             }
    36         }
    37     }
    38 
    39 
    40     if (!directory.rmdir(QDir::toNativeSeparators(directory.path())))
    41     {
    42         qDebug() << "remove dir" << directory.path() << " faild!";
    43         error = true;
    44     }
    45     return !error;
    46 }
  • 相关阅读:
    19. Remove Nth Node From End of List
    18. 4Sum
    16. 3Sum Closest
    15. 3Sum
    17. Letter Combinations of a Phone Number
    A Network-based End-to-End Trainable Task-oriented Dialogue System
    14. Longest Common Prefix
    36. Valid Sudoku
    29. Divide Two Integers
    32. Longest Valid Parentheses
  • 原文地址:https://www.cnblogs.com/felix-wang/p/6248317.html
Copyright © 2011-2022 走看看