November 6, 2011
Qt Remove Directory
Qt Remove Directory
Qt have rich QDir class provides access to directory structures and their contents. QDir have function QDir::rmdir to remove directory. The directory must be empty for rmdir() to succeed. This Qt Remove Directory function Returns true if successful, otherwise returns false. The problem is how Qt Remove Directory if that directory not empty? If you have non empty directory, you can use this Qt Remove Directory function to remove non empty directory with all subdirectory. This is a Qt Remove Directory function :
bool frameWorkspace::removeDirectory( QDir dir ) { bool ok = dir.exists(); if ( ok ) { QFileInfoList entries = dir.entryInfoList( QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files ); foreach ( QFileInfo entryInfo, entries ) { QString path = entryInfo.absoluteFilePath(); if ( entryInfo.isDir() ) { if ( ! removeDirectory( QDir( path ) ) ) { ok = false; break; } } else { QFile file( path ); if ( ! file.remove() ) { ok = false; break; } } } } if ( ok && ! dir.rmdir( dir.absolutePath() ) ) ok = false; return ok; }
You can call this Qt Remove Directory function with this method :
QDir dir; dir.setPath("/your/sample/directory"); removeDirectory(dir);
If you have any question or method about this Qt Remove Directory, please share to us.
Source : http://www.archivum.info/qt-interest@trolltech.com/2009-09/00049/Re-%28Qt-interest%29-What-should-I-use-to-delete-non-empty-directories.html5 Comments
I think, you should use QFile::remove( path ), otherwise looks fine to me.
Hi AnCoward,
Thank you for your comment. I think we can not use your method. I use your method and fail. The problem for this posting is how to remove not empty directory.
This is like how to remove directory with sub directory and every directory is not empty. I am figured like this :
/a
/a/b
/a/b/tes1.txt
/a/c/
a/c/tes2
We want to remove directory ‘a’ with all subdirectory and file in a.
If you read QFile::remove( path ) documentation :
bool QFile::remove ( const QString & fileName ) [static]
This is an overloaded function.
Removes the file specified by the fileName given.
So, this function is to remove a file, not directory. Thank you for visiting my site
I meant instead of:
QFile file( path );
if ( ! file.remove() )
I’d use static method:
if ( ! QFile::remove( path ) )
But whatever, doesn’t really matter.
I’m actually surprised that there is no recursive remove in QDir, people have to implement it for themselves every time.
Yet another version in KDE sources:
http://quickgit.kde.org/?p=kdevplatform.git&a=blob&hb=HEAD&f=util/fileutils.cpp
> Thank you for visiting my site
You’re welcome, found this post in Google+ sparks.
Thank you! Your code works fine except that it does not handle hidden directories (very common under UNIX as directories starting with a dot). If you append QDir::Hidden to the list of flags then it handles these too 🙂
Hi Kristian,
Thank you for visiting my site and give other idea about how to remove hidden Dir in Unix/Linux 🙂