Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a non empty folder in Qt

Tags:

qt

qt4

How to remove a non-empty folder in Qt.

like image 860
Viku Avatar asked Jun 15 '12 12:06

Viku


People also ask

How do I delete a folder in Qt?

You can call this Qt Remove Directory function with this method : QDir dir; dir. setPath("/your/sample/directory"); removeDirectory(dir);

How will you remove a directory tree even it is not empty without using Rmdir?

To remove a directory that is not empty, use the rm command with the -r option for recursive deletion. Be very careful with this command, because using the rm -r command will delete not only everything in the named directory, but also everything in its subdirectories.

How do I delete a folder in CPP?

Try use system " rmdir -s -q file_to_delte ". This will delete the folder and all files in it.


2 Answers

If you're using Qt 5, there is QDir::removeRecursively().

like image 93
hpsMouse Avatar answered Oct 03 '22 02:10

hpsMouse


Recursively delete the contents of the directory first. Here is a blog post with sample code for doing just that. I've included the relevant code snippet.

bool removeDir(const QString & dirName) {     bool result = true;     QDir dir(dirName);      if (dir.exists()) {         Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {             if (info.isDir()) {                 result = removeDir(info.absoluteFilePath());             }             else {                 result = QFile::remove(info.absoluteFilePath());             }              if (!result) {                 return result;             }         }         result = QDir().rmdir(dirName);     }     return result; } 

Edit: The above answer was for Qt 4. If you are using Qt 5, then this functionality is built into QDir with the QDir::removeRecursively() method .

like image 39
Judge Maygarden Avatar answered Oct 03 '22 02:10

Judge Maygarden