remove directory empty or not empty
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)
Code:
QString artistDirectory
(path
+ "/" + artist
);
directoryTodelete.rmpath(artistDirectory);
Re: remove directory empty or not empty
You will need to write a recursive function to do this.
Something like this
Code:
{
if(fileInfo.isDir()){
for(int i = 0; i < fileList.count(); ++i){
remove(fileList.at(i));
}
rmpath(path);
}
else{
}
}
Re: remove directory empty or not empty
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:
Code:
if(fileInfo.isDir())
{
for(int i = 0; i < fileList.count(); ++i)
{
QMessageBox::information(this,
"fileList.at(i)",fileList.
at(i
));
remove(fileList.at(i));
}
rmpath(pathTest);
}
else
{
}
Re: remove directory empty or not empty
You are not calling QDir::rmdir() anywhere, you only delete files.
Re: remove directory empty or not empty
try replacing
with
Also, I see that the function is not recursive.
Re: remove directory empty or not empty
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?
Code:
QString pathTest
= "C:/temp/artist1";
if(fileInfo.isDir())
{
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
{
}
Re: remove directory empty or not empty
Something like this should work.
Code:
void someFunction()
{
QString pathTest
= "C:/temp/artist1";
remove(pathTest);
}
{
if(fileInfo.isDir()){
for(int i = 0; i < fileList.count(); ++i){
remove(fileList.at(i));
}
dir.rmdir(path);
}
else{
}
}
I thought the code was pretty much self-explanatory
Re: remove directory empty or not empty
Hi munna :crying:
Have you test it?I am not able to delete any folder..
Are you shure this should work?Its not working:
Code:
void Manage::deleteArtist()
{
.
.
QString artist
= ui.
delete_artist_cb->currentText
();
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
) {
if(fileInfo.isDir())
{
QDir dir
(artistDirectory
);
for(int i = 0; i < fileList.count(); ++i)
{
remove(fileList.at(i));
}
dir.rmdir(artistDirectory);
}
else
{
QFile::remove(artistDirectory
);
}
}
Re: remove directory empty or not empty
Your removeArtistDirectory function is not recursive
Inside the for loop, replace
Code:
remove(fileList.at(i));
with
Code:
removeArtistDirectory(fileList.at(i));
By the way, is this code not giving you any error?
Re: remove directory empty or not empty
Hi :crying:
No i didnt get a error message from MINGW :crying:
With following code the for loop never end.
Why??
Code:
void Manage
::removeArtistDirectory(const QString &artistDirectory
) {
if(fileInfo.isDir())
{
QDir dir
(artistDirectory
);
for(int i = 0; i < fileList.count(); ++i)
{
removeArtistDirectory(fileList.at(i));
}
dir.rmdir(artistDirectory);
}
else
{
QFile::remove(artistDirectory
);
}
}
Re: remove directory empty or not empty
Try excluding "."'s and ".."'s:
Re: remove directory empty or not empty
Quote:
Originally Posted by
raphaelf
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)
Code:
QString artistDirectory
(path
+ "/" + artist
);
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!!
Code:
bool qt_unlink
(QString fullFileName
) {
if ( is_file( fullFileName ) ) {
if (f.remove()) {
return true;
}
}
return false;
}
{
SqlLog("order to delete dir:"+d+" ");
if (dir.exists())
{
const QFileInfoList list = dir.entryInfoList();
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);
}
}
Re: remove directory empty or not empty
:D :D
Thank you very much!!
Thanks to all :p
Have a nice Day :rolleyes: