PDA

View Full Version : remove directory empty or not empty



raphaelf
25th October 2006, 13:48
Hi everybody,

I have a folder "artistx". This folder could be empty or not.
I would like to delete folder "artistx" and if it contains files or folder or not, i would like to delete it.
I tried like this: (I can just remove the folder if the folder is empty)



QString artistDirectory(path + "/" + artist);
QMessageBox::information(this,"",artistDirectory);
QDir directoryTodelete;


directoryTodelete.rmpath(artistDirectory);

munna
25th October 2006, 13:59
You will need to write a recursive function to do this.

Something like this




void remove(const QString &path)
{
QFileInfo fileInfo(path);
if(fileInfo.isDir()){
QDir dir(path);
QStringList fileList = dir.entryList();
for(int i = 0; i < fileList.count(); ++i){
remove(fileList.at(i));
}
rmpath(path);
}
else{
QFile::remove(path);
}
}

raphaelf
26th October 2006, 07:44
Hi,
I have a folder under c:\temp with the name aa1.
This folder contains a folder qq1. And qq1 contains a file.
I was not able to delete aa1, qq1 and the file.
What is wrong here:



QString pathTest = "C:/temp/aa1";
QFileInfo fileInfo(path);
if(fileInfo.isDir())
{
QDir dir(pathTest);
QMessageBox::information(this,"pathTest",pathTest);
QStringList fileList = dir.entryList();

for(int i = 0; i < fileList.count(); ++i)
{
QMessageBox::information(this,"fileList.at(i)",fileList.at(i));
remove(fileList.at(i));
}
rmpath(pathTest);
}
else
{
QFile::remove(pathTest);
}

wysota
26th October 2006, 08:04
You are not calling QDir::rmdir() anywhere, you only delete files.

munna
26th October 2006, 09:58
try replacing




rmpath(pathTest);



with




dir.rmdir(path);



Also, I see that the function is not recursive.

raphaelf
26th October 2006, 10:45
Hi :crying:

Why can i not delete artist1 under c:\temp??The folder artist1 contains a folder album1 and album1 contains a file a.jpg.

What is wrong?



QString pathTest = "C:/temp/artist1";
QFileInfo fileInfo(pathTest);
if(fileInfo.isDir())
{
QDir dir(pathTest);
QMessageBox::information(this,"pathTest",pathTest);
QStringList fileList = dir.entryList();

for(int i = 0; i < fileList.count(); ++i)
{
QMessageBox::information(this,"fileList.at(i)",fileList.at(i));
remove(fileList.at(i));
}
dir.rmdir(pathTest);
}
else
{
QFile::remove(pathTest);
}

munna
26th October 2006, 11:14
Something like this should work.




void someFunction()
{
QString pathTest = "C:/temp/artist1";
remove(pathTest);
}

void remove(const QString &path)
{
QFileInfo fileInfo(path);
if(fileInfo.isDir()){
QDir dir(path);
QStringList fileList = dir.entryList();
for(int i = 0; i < fileList.count(); ++i){
remove(fileList.at(i));
}
dir.rmdir(path);
}
else{
QFile::remove(path);
}
}



I thought the code was pretty much self-explanatory

raphaelf
26th October 2006, 12:23
Hi munna :crying:

Have you test it?I am not able to delete any folder..

Are you shure this should work?Its not working:



void Manage::deleteArtist()
{
.
.
QString artist = ui.delete_artist_cb->currentText();
QString path;
QSqlQuery select_sound_path("select value from config_tbl where config ='dsm_path'");
while(select_sound_path.next())
{
path = select_sound_path.value(0).toString();
}
QString artistDirectory(path + "/" + artist);
removeArtistDirectory(artistDirectory);
}

void Manage::removeArtistDirectory(const QString &artistDirectory)
{
QMessageBox::information(this,"","remove function");
QMessageBox::information(this,"",artistDirectory);
QFileInfo fileInfo(artistDirectory);
if(fileInfo.isDir())
{
QDir dir(artistDirectory);
QStringList fileList = dir.entryList();
for(int i = 0; i < fileList.count(); ++i)
{
QMessageBox::information(this,"",fileList.at(i));
remove(fileList.at(i));
}
dir.rmdir(artistDirectory);
}
else
{
QFile::remove(artistDirectory);
}
}

munna
26th October 2006, 12:29
Your removeArtistDirectory function is not recursive

Inside the for loop, replace




remove(fileList.at(i));



with




removeArtistDirectory(fileList.at(i));



By the way, is this code not giving you any error?

raphaelf
26th October 2006, 13:58
Hi :crying:

No i didnt get a error message from MINGW :crying:

With following code the for loop never end.
Why??



void Manage::removeArtistDirectory(const QString &artistDirectory)
{

QFileInfo fileInfo(artistDirectory);
if(fileInfo.isDir())
{
QDir dir(artistDirectory);
QStringList fileList = dir.entryList();
for(int i = 0; i < fileList.count(); ++i)
{
QMessageBox::information(this,"",fileList.at(i));
removeArtistDirectory(fileList.at(i));
}
dir.rmdir(artistDirectory);
}
else
{
QFile::remove(artistDirectory);
}
}

jpn
26th October 2006, 15:18
Try excluding "."'s and ".."'s:


QStringList fileList = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot);

patrik08
26th October 2006, 16:55
Hi everybody,

I have a folder "artistx". This folder could be empty or not.
I would like to delete folder "artistx" and if it contains files or folder or not, i would like to delete it.
I tried like this: (I can just remove the folder if the folder is empty)



QString artistDirectory(path + "/" + artist);
QMessageBox::information(this,"",artistDirectory);
QDir directoryTodelete;


directoryTodelete.rmpath(artistDirectory);



Remove line SqlLog & insert the full path to dir be remove....

DownDir_RM("c:\\") remove all file!!
Bee sure evry time the fulla path insert!!








bool qt_unlink(QString fullFileName)
{
QFile f( fullFileName );
if ( is_file( fullFileName ) ) {
if (f.remove()) {
return true;
}
}
return false;
}

void DownDir_RM(const QString d)
{
QDir dir(d);
SqlLog("order to delete dir:"+d+" ");
if (dir.exists())
{
const QFileInfoList list = dir.entryInfoList();
QFileInfo fi;
for (int l = 0; l < list.size(); l++)
{
fi = list.at(l);
if (fi.isDir() && fi.fileName() != "." && fi.fileName() != "..")
DownDir_RM(fi.absoluteFilePath());
else if (fi.isFile())
{
bool ret = qt_unlink(fi.absoluteFilePath());
if (!ret)
SqlLog("Can't remove: " + fi.absoluteFilePath() + " (write-protect?)");
}

}
SqlLog("Remove: " + d + " ");
dir.rmdir(d);

}
}

raphaelf
27th October 2006, 07:30
:D :D
Thank you very much!!

Thanks to all :p

Have a nice Day :rolleyes: