PDA

View Full Version : How to remove empty folders?



merry
5th August 2008, 06:07
Hi all

Working on Qt4.2 on my mac intel machine.



void MainWindow::ClearDir(QString dirRoot)
{
ClearAll(dirRoot);
QDir d(dirRoot);
d.cdUp();
d.rmdir(dirRoot);
}

/*Function for erasing Folder*/

void MainWindow:: ClearAll(QString dirViewPath)
{
QDir dirPath(dirViewPath);
QList<QFileInfo> infLst = dirPath.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);

foreach(QFileInfo finfo, infLst )
{
if(finfo.isDir() )
{
ClearAll(finfo.absoluteFilePath());
dirPath.rmdir(finfo.absoluteFilePath());
}
else
{ //file
QString fileName = dirViewPath + "/" + finfo.fileName();
dirPath.remove(finfo.absoluteFilePath());
}
}
}


Using this code, I try to remove the existing dir selected using QFileDialog, I will be able to remove all the files but empty folders are left.

spirit
5th August 2008, 08:52
are you really sure that the directory which you try to remove is empty?

bool QDir::rmdir ( const QString & dirName ) const
Removes the directory specified by dirName.
The directory must be empty for rmdir() to succeed.
Returns true if successful; otherwise returns false.
See also mkdir().

merry
5th August 2008, 10:22
Thanks for the replying.

When i am trying remove directories, it returns false...

What it mean, that the directories are not empty..

For eg. i am having Folder F0 which contains sub folder F1 and F2 Which further contains some files f1,f2,f3,f4..

using the above code, written in the previous mail, i was able to remove all the files f1,f2,f3,f4 but folders F0, F1 and F2 are still there, is am doing something wrong or is there any other alternative...

spirit
5th August 2008, 10:50
try this code


MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
m_pb = new QPushButton(tr("Clear"));
connect(m_pb, SIGNAL(clicked()), SLOT(clearFolder()));
setCentralWidget(m_pb);
}

void MainWindow::clearFolder()
{
clearFolder("E:/f0");
}

void MainWindow::clearFolder(const QString &folder)
{
QDir dir(folder);
QStringList entries = dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
foreach (QString entry, entries) {
QFileInfo fileInfo(folder + "/" + entry);
if (fileInfo.isFile() && !QFile::remove(fileInfo.filePath())) {
qWarning() << "can't remove '" << fileInfo.filePath() << "'";
return;
} else if (fileInfo.isDir())
clearFolder(fileInfo.filePath());
}
if (!dir.rmdir(dir.absolutePath()))
qWarning() << "can't remove '" << dir.absolutePath() << "'";
}

merry
5th August 2008, 11:26
I tried above code , but it also wont able to remove dirs.

spirit
5th August 2008, 11:33
very strange, I have run this code and it worked.

spirit
5th August 2008, 11:38
maybe you don't have permissions to remove dirs?

merry
5th August 2008, 12:07
Actually, When i create a new blank folder than it is able to remove ,If i open an existing directory which contains subdirs also using QFileDialog , then it wont able to remove..

spirit
5th August 2008, 12:13
maybe dir path is absolute?

merry
5th August 2008, 12:49
Yaa the dir path is absolute.

merry
6th August 2008, 10:00
If the path is absolute , then how can i remove direcctories..

spirit
6th August 2008, 10:07
can you show path and dirs structure?

merry
6th August 2008, 10:35
The Path is "/private/var/root/Desktop/Root"
"/private/var/root/Desktop/Root/GIF"

spirit
6th August 2008, 13:00
looks like correct dir path.
maybe dir has a rea-only files or it's read-only. did you check it?

merry
6th August 2008, 13:05
Yaa I check, the files are not read only, Files and dirs both are Read & Write.

spirit
6th August 2008, 14:26
I've just tested code from my post under Linux, it works fine.
try to put qDebug() and output result of dir and files removing.

merry
7th August 2008, 09:33
Yaa, When i run the code under Windows then it removes all the files and folders, and in MAC it remove all the files and also folders that are under root dir except the root dir,dont understand wat to do?

spirit
7th August 2008, 09:38
yup, under Linux the same situation, directory "/private" can't be removed, because you have no rights to do this, but I try under "root" to delete and all content and "/private" directory was removed successfully.

merry
7th August 2008, 09:40
no, I am login as a root only..

yogeshgokul
16th December 2008, 09:14
That recursive code worked for me,, thanxx a lot @ spirit.