PDA

View Full Version : How to erase all the files and folders in the Selected existing directory



merry
25th July 2007, 14:53
Hi all

Working on Qt4.2

Problem :

I want to delete all the flles and folders contained in the directory that is selected by QFileDialog.

Pls help , if anyboby know.......


Thanx

marcel
25th July 2007, 15:05
QDir dir = ...//initialize with the return of the file dlg
void recursiveDelete(const QDir &dir )
{
QList<QFileInfo> infLst = dir.entryInfoList();
foreach(QFileInfo finfo, infList )
{
if(fInfo.isDir() )
{
QDir subDir(finfo.absolutePath());
recursiveDelete(subDir);
dir.rmdir(subDir);
}
else { //file
dir.remove(finfo.absolutePath());
}
}
}
This code is not tested so you may have to polish it a bit :).

But the idea is correct.

Regards

spud
25th July 2007, 16:49
You should specify QDir::NoDotAndDotDot. I.e.


QList<QFileInfo> infLst = dir.entryInfoList(QDir::NoDotAndDotDot);

you should also be very careful with this function. Otherwise you'll get into trouble faster than you can say
recursiveDelete(QDir("/"))

marcel
25th July 2007, 16:52
You should specify QDir::NoDotAndDotDot. I.e.


QList<QFileInfo> infLst = dir.entryInfoList(QDir::NoDotAndDotDot);
you should also be very careful with this function. Otherwise you'll get into trouble faster than you can say
recursiveDelete(QDir("/"))

I already said this code is not tested.
It is not meant just to be copy-pasted in an application.
It is just a draft.

So, whoever decides to use it should adapt it to their own needs.

Regards

merry
26th July 2007, 07:47
Thanx

It works , It deletes all the files contained in the folder,

but is there any other way , that after deleting all the files of the folder it also deletes that folder .


Thanx

marcel
26th July 2007, 07:58
Yes, just call rmdir for the original QDir, that you pass to the funciton.
You have to call this after the function returns.

Regards

merry
26th July 2007, 08:21
Thanx

I do like this only ,but it wont works...

Pls view code...



void Form1::DeleteData(QString dirViewPath)
{
QDir dirPath(dirViewPath);
QList<QFileInfo> infLst = dirPath.entryInfoList();
for(int i=0;i<infLst.size();i++)
{
QFileInfo strFileInfo = infLst.at(i);
QString strFileName = strFileInfo.fileName();
if(!(strFileInfo.fileName() == "." || strFileInfo.fileName() == ".."))
{
if(strFileInfo.isDir())
{
QString strFilePath = dirViewPath + "/" + strFileInfo.fileName();
QString fileName = strFileInfo.fileName();
DeleteData(strFilePath);
dirPath.rmdir(dirViewPath);
}
else
{
QString fileName = dirViewPath + "/" + strFileInfo.fileName();
QFile file1(fileName);
file1.remove();
}
}
}
}

marcel
26th July 2007, 08:36
DeleteData does not work?
Or removing the parent dir?
I assume you call it like this:


...
QString dirViewPath = ...
DeleteData(dirViewPath);
//Also add the following two lines:
QDir dir(dirViewPath);
dir.rmdir();
...


Or is it a slot?

Regards

merry
26th July 2007, 08:47
HI

DeleteData does not work?
Or removing the parent dir?
I assume you call it like this:
...
QString dirViewPath = ...
DeleteData(dirViewPath);
//Also add the following two lines:
QDir dir(dirViewPath);
dir.rmdir();


Pls view the code I sent you in the last post,I think I had added the above two lines..

thanx

marcel
26th July 2007, 09:03
Oh, yes, I see now.
That will not work because at that point the parent dir is possible not to be empty, therefore the call will fail.

Do it like I have shown you.

Another solution is:


void Form1::DeleteData(String dirViewPath)
{
...
//function body
...

if(!dirPath.count()==2) //"." and ".." -- might not be the case. if not,. test with 0
{
dirPath.cdUp();
dirPath.rmdir(dirViewPath);
}
}


This will work. I forgot to mention that you have to go one level above dirViewPath, in order to delete it.

Regards

merry
26th July 2007, 09:17
Thanx for the reply

can u pls explain me why u had used this line


if(!dirPath.count()==2)

and also I had used this also


if(!dirPath.count()==2) //"." and ".." -- might not be the case. if not,. test with 0
{
dirPath.cdUp();
dirPath.rmdir(dirViewPath);
}

but it also not works....

marcel
26th July 2007, 09:19
:) I am at work and I write these in a hurry.
Remove the "!".
I added those because count might return 2 even if the dir is empty( 2 for . and .. ).

Regards

merry
26th July 2007, 09:23
Thanx Marcel

But it also not works

marcel
26th July 2007, 09:27
This means that "." and ".." are ignore by count().
Compare with 0 instead of 2 and you'll get it done.

Regards

merry
26th July 2007, 10:17
Sorry

It again not works

regards

marcel
26th July 2007, 10:28
Then just delete it outside the function after the directory's contents are all removed.
I think you don't "believe in urself" :).

Regards

merry
26th July 2007, 10:33
Thank You very much for telling me that

I dont beleive in myself

merry
27th July 2007, 05:51
hi

Is there anybody Who tells me how to delete folders after the deletion of files..

Actually all the files contained in the folders are deleted but only the blank folders are left

how can i delete folders , pls help....

I used this also but nothing happens



if(dirPath.count()==0)
{
dirPath.cdUp();
dirPath.rmdir(dirViewPath);
}

Thanx

marcel
27th July 2007, 05:59
Do it "urself"!
:)

Regards

merry
27th July 2007, 06:05
Thanx Marcel for the reply

Ok I 'll do it myself only.:)

marcel
30th July 2007, 08:53
Then just delete it outside the function after the directory's contents are all removed.

Regards

Have you tried this?
Can you post a bigger part of the code?
But first try deleting the dir after you call the recursive function.

Regards

merry
30th July 2007, 09:05
Hi Marcel :)

Really I tried it lots & lots of times but it does not remove directories,It removes all the files but not directories .

Ok Pls view my code , and tell me is I m doin something wrong.


void Form1::DeleteData(QString dirViewPath)
{
QDir dirPath(dirViewPath);
if (dirPath.exists())
{
QList<QFileInfo> infLst = dirPath.entryInfoList();
for(int i=0;i<infLst.size();i++)
{
QFileInfo strFileInfo = infLst.at(i);
QString strFileName = strFileInfo.fileName();
if(!(strFileInfo.fileName() == "." || strFileInfo.fileName() == ".."))
{
if(strFileInfo.isDir())
{
QString strFilePath = dirViewPath + "/" + strFileInfo.fileName();
QString fileName = strFileInfo.fileName();
DeleteData(strFilePath);
}
else
{
unsigned char *pBuff;
FILE *fh = NULL;
QString fileName = dirViewPath + "/" + strFileInfo.fileName();
QFile file1(fileName);
long SizeToRead =fileName.size();
fh=fopen((const char*)fileName.toAscii() ,"wb+");
pBuff = new unsigned char[SizeToRead];
QString str;
str = comboBox->itemText(comboBox->currentIndex());
bool ok;
int val= str.toInt(&ok,36);
memset(pBuff,val,SizeToRead);
fwrite((void*)pBuff,SizeToRead,1,fh);
delete []pBuff;
pBuff=NULL;
fclose(fh);
file1.remove();
}
}
}
dirPath.cdUp();
dirPath.rmdir(dirViewPath);
}
}


Thanx

marcel
30th July 2007, 09:09
But I really wanted to know where you call DeleteData.
Could you post that part of the code?

Regards

merry
30th July 2007, 09:54
Hi..

I m sending u both .h and .cpp files Pls view this code.

Form1.h

class Form1:public QDialog,private Ui::Form1
{
Q_OBJECT
public:
Form1::Form1(QWidget *parent): QDialog(parent)
{
setupUi(this);
connect(File,SIGNAL(clicked()),this,SLOT(ShowFileD ialog()));
connect(Erase,SIGNAL(clicked()),this,SLOT(CallDele teData()));
};
private slots:
void ShowFileDialog();
void DeleteData(QString);
void CallDeleteData();
protected:
QString m_file;
};

Form1.cpp



void Form1::ShowFileDialog()
{
m_file=QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);

filename->setText(m_file);

}

void Form1::CallDeleteData()
{
DeleteData(m_file);
}
////////////////////////////////////////////////////////////////////////

/*Function for erasing*/

void Form1::DeleteData(QString dirViewPath)
{
QDir dirPath(dirViewPath);
if (dirPath.exists())
{
QList<QFileInfo> infLst = dirPath.entryInfoList();
for(int i=0;i<infLst.size();i++)
{
QFileInfo strFileInfo = infLst.at(i);
QString strFileName = strFileInfo.fileName();
if(!(strFileInfo.fileName() == "." || strFileInfo.fileName() == ".."))
{
if(strFileInfo.isDir())
{
QString strFilePath = dirViewPath + "/" + strFileInfo.fileName();
QString fileName = strFileInfo.fileName();
DeleteData(strFilePath); //recursive Calling
}
else
{
unsigned char *pBuff;
FILE *fh = NULL;
QString fileName = dirViewPath + "/" + strFileInfo.fileName();
QFile file1(fileName);
long SizeToRead =fileName.size();
fh=fopen((const char*)fileName.toAscii() ,"wb+");
pBuff = new unsigned char[SizeToRead];
QString str;
str = comboBox->itemText(comboBox->currentIndex());
bool ok;
int val= str.toInt(&ok,36);
memset(pBuff,val,SizeToRead);
fwrite((void*)pBuff,SizeToRead,1,fh);
delete []pBuff;
pBuff=NULL;
fclose(fh);
file1.remove();
}
}
}
dirPath.cdUp();
dirPath.rmdir(dirViewPath);
}
}

marcel
30th July 2007, 10:02
In CallDeleteData():

void Form1::CallDeleteData()
{
DeleteData(m_file);
QDir dir(m_file);
if( dir.cdUp() )
{
dir.rmdir(m_file);
}
}


Also, remove lines 60 and 61 from DeleteData. They are not needed anymore.

Regards

merry
30th July 2007, 10:15
Thanx Marcel for the reply.

I tried it , but directories are still there..

DeleteData function is recursively Calling.....

Thanx...

marcel
30th July 2007, 10:22
Thanx

It works , It deletes all the files contained in the folder,

but is there any other way , that after deleting all the files of the folder it also deletes that folder .


Thanx

But you said that the contained files and folders are deleted.
I understood that your only problem is the parent folder. You couldn't delete that.

Which one is it?

Regards

merry
30th July 2007, 10:27
Actually I said that it deletes all the Files that are in the folders , and empty folders are left.....,

eg..

If there is a folder name "dir1" it further contains files(F1 , F2) and folder dir2.

then F1 and F2 are deleted but empty dir1 and dir2 folders are left....

My question is how can i delete empty folders?

Thanx

marcel
30th July 2007, 10:33
But you messed up the code.
In the case you run into a directory:


QDir subDir(finfo.absolutePath());
dir.rmdir(subDir);


You removed that.
It should work, as long as the directory is not used/empty.
Couldn't you debug it to see exactly what is wrong?

merry
30th July 2007, 10:43
QDir subDir(finfo.absolutePath());
dir.rmdir(subDir);


Are u sure it works..
The function is like this only...

QDir::rmdir ( const QString & dirName )

and subDir is of QDir type...

and parameters passed in the function are of QString type...


Thanx

marcel
30th July 2007, 10:46
QDir subDir(finfo.absolutePath());
dir.rmdir(subDir);
Are u sure it works..
The function is like this only...

QDir::rmdir ( const QString & dirName )

and subDir is of QDir type...

and parameters passed in the function are of QString type...

Thanx
Well, fix it!
Pass finfo.absolutePath() to rmdir.
subdir has to be empty at that moment. so,a rmdir from the parent should work.

What about the debugging?

marcel
30th July 2007, 11:04
Here it is. I just tested it:

This is where it starts( the wrapper):


void qttest::clearAll()
{
QString dirRoot = "c:\\testdir";
clearAll(dirRoot);
QDir d(dirRoot);
d.cdUp();
d.rmdir(dirRoot);
}




This is the actual function:


void qttest::clearAll(QString dirPath)
{
QDir dir(dirPath);
QList<QFileInfo> infLst = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
foreach(QFileInfo finfo, infLst )
{
if(finfo.isDir() )
{
clearAll(finfo.absoluteFilePath());
dir.rmdir(finfo.absoluteFilePath());
}
else
{ //file
dir.remove(finfo.absoluteFilePath());
}
}
}



Regards.

merry
30th July 2007, 11:43
Hi Marcel


Can u pls tell me in function


void qttest::clearAll()
{
QString dirRoot = "c:\\testdir";
clearAll(dirRoot);
QDir d(dirRoot);
d.cdUp();
d.rmdir(dirRoot);
}


Why you used this(4th line of the above code) that is:


clearAll(dirRoot);

Thanx

marcel
30th July 2007, 11:47
You should pass there the directory returned by the file dialog.
clearAll() without any parameters is just an overloaded variant of the second, provided for convenince.

It is really not that different than the first version.
I just passed a folder that I created on my hard disk.

And don't say it doesn't work because it does. If you don't mess up the code, then it will work for you too.

Regards

merry
30th July 2007, 12:43
Hi Marcel

Thanx Marcel for helping me so much

Pls dont get angry on me....:(

I think I am not messing the code now , It is same as the code u sent to me , Is'nt it...

But it also deletes the files but not folders.....



void Form1::ShowFileDialog()
{
m_file=QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);



filename->setText(m_file);

}

void Form1::DeleteData()
{
QString dirRoot = m_file;
DeleteData(dirRoot);
QDir d(dirRoot);
d.cdUp();
d.rmdir(dirRoot);
}
////////////////////////////////////////////////////////////////////////

/*Function for erasing*/

void Form1::DeleteData(QString dirViewPath)
{
QDir dirPath(dirViewPath);
if (dirPath.exists())
{
QList<QFileInfo> infLst = dirPath.entryInfoList();
for(int i=0;i<infLst.size();i++)
{
QFileInfo strFileInfo = infLst.at(i);
QString strFileName = strFileInfo.fileName();
if(!(strFileInfo.fileName() == "." || strFileInfo.fileName() == ".."))
{
if(strFileInfo.isDir())
{
DeleteData(strFileInfo.absoluteFilePath());
dirPath.rmdir(strFileInfo.absoluteFilePath());
}
else
{

dirPath.remove(strFileInfo.absoluteFilePath());

}
}

}
}
}


Thanx

marcel
30th July 2007, 12:53
Are you sure you have permissions to delete the directories?
Are you running as root or as a normal user?

merry
30th July 2007, 12:56
I m running as a normal user

marcel
30th July 2007, 13:02
Well, could you try running it as root?

merry
30th July 2007, 13:24
I tried it running as a root

It works , but for that directories only which contains files only.

not for that directories which contains both files and folders ..


Thanx

marcel
30th July 2007, 13:33
Sorry, but can't help you. I really can't understand what's going on.
I tested it on the following structure. Every directory in there had 1,2 or three files in it.
So I can't say what is going on in your case.

Regards

marcel
30th July 2007, 14:00
One more thing:
Make sure there isn't any browser opened on any of the directories you're trying to delete.
Actually, close all the browsers(file system browsers) and run the program.

Regards

merry
30th July 2007, 14:28
Thanx Marcel

It works....

But tell me one thing

It deletes all the files and folders that are created by me at that time only..

If i delete directories that are already existed , in that case it deletes files only and not folders , Is it that I dont have permission for deleting the directories.

Thanx

marcel
30th July 2007, 14:55
Depends on what you need the program.
Usually these programs must be executed as root, with full privileges.
If your application runs under a normal user and creates some directories in your home folder, then it can delete them. I don't see any reason why it would fail.

Regards