You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, December 18th 2007, 11:19am

how to remove a directory which is not empty?

if a directory is not empty , how to remove it ?
can anyone help me ?
thanks!

2

Tuesday, December 18th 2007, 12:24pm

RE: how to remove a directory which is not empty?

at least : what is your system (linux,windows,...)
Nicolas

3

Wednesday, December 19th 2007, 2:31am

RE: how to remove a directory which is not empty?

linux FC5
QFile and QDir seems to only can remove a directory which is empty.

CrackedMind

Beginner

  • "CrackedMind" is male

Posts: 35

Location: Russia

Occupation: just programmer

  • Send private message

4

Wednesday, December 19th 2007, 8:04am

recursively QDir::remove, and QDir::rmdir :)
So you can code punk?

5

Wednesday, December 19th 2007, 8:37am

as specified in QDir documentation

Quoted


The directory must be empty for rmdir() to succeed.

why not try system function:

Source code

1
system( "rm -rf ..." );
Nicolas

6

Wednesday, December 19th 2007, 8:41am

yes, you are right . system call is ok . however , I think , it is not the best way.
Qt should encapsure these calls into its class. unfortunately , I can not find it out.

thanks .

7

Wednesday, December 19th 2007, 8:42am

Quoted

Originally posted by CrackedMind
recursively QDir::remove, and QDir::rmdir :)


.....

8

Wednesday, December 19th 2007, 8:49am

I think that when using tools like Qt, ...

we should not forget that these tools use the underlaying system and for some simple things, it's often better to directly use the underlaying system
Nicolas

9

Wednesday, December 19th 2007, 9:06am

I agree with you for almost.However , in this case , if I want to delete something in the filesystem ,
my code will like these:

void rm( const QString &filepath )
{
if (isFile(filepath))
QFile::remove(filepath);
else
{
system("rm -rf "+filepath );
}
}

I think , this is uglily. half is wrote by Qt , half is wrote using C.
of cause we can implement it like this:
void rm( const QString &filepath )
{
system("rm -rf "+filepath );
}

Does it mean Qt do not support delete a directory ? I think , the answer is no. but how to achieve this?

10

Wednesday, December 19th 2007, 9:12am

in your code

Source code

1
"rm -rf " + filepath

is a QString and need to bee converted to a char pointer to be passed to system function

Source code

1
("rm -rf " + filepath).toAscii().data()

perhaps you also need use QString::toLatin1 ...
have a look at QString documentation
Nicolas

This post has been edited 1 times, last edit by "Nicolas SOUCHON" (Dec 19th 2007, 9:13am)


11

Wednesday, December 19th 2007, 9:22am

as you say , I have to convert QString into char * ...