Delete a directory with all sub-directories an files

D

Dirk Einecke

Hi.

I would like to delete a directory. In this directory are other
directories an files. So I have to delete this subdirectories an files
at first. Now I search a good solution to solve this problem. My first
idea was to look in the given directory and if there are subdirectories
to look in this directories... Is this the right way? Is there a ready
function for this little problem? Or has anybody written a function for it?

greetings
Dirk Einecke
 
S

Simon Strandgaard

I would like to delete a directory. In this directory are other
directories an files. So I have to delete this subdirectories an files
at first. Now I search a good solution to solve this problem. My first
idea was to look in the given directory and if there are subdirectories
to look in this directories... Is this the right way? Is there a ready
function for this little problem? Or has anybody written a function for it?

require 'fileutils'
FileUtils.rm_rf("somerandomdir")


#untested
 
D

Dirk Einecke

Hi Simon.

Simon said:
require 'fileutils'
FileUtils.rm_rf("somerandomdir")

Sorry - I have not this library (and it is not possible to install this
library). But I've ftools. Can me help this library?

greetings
Dirk Einecke
 
M

Michael C. Libby

Sorry - I have not this library (and it is not possible to install this
library). But I've ftools. Can me help this library?

You could update to Ruby 1.8+ where fileutils.rb is a standard module.
If that doesn't work you can simply put a copy of fileutils.rb from a
recent distribution into your working script directory and include it
from there. No need to "install" it.

And if for some reason that isn't possible, then you can always just
copy the code straight from the module into your script. In this case it
looks like you'd need to copy in less than 25 lines of code for the
rm_rf command and the rm_r command. The only time I wouldn't do this is
if there were license concerns. In any case, the fileutils.rb module has
a great example of how to do this. :)

-michael
 
D

Dirk Einecke

Hi Michael.
You could update to Ruby 1.8+ where fileutils.rb is a standard module.

That's not possible to because Ruby is running on the server of my ISP.
If that doesn't work you can simply put a copy of fileutils.rb from a
recent distribution into your working script directory and include it
from there. No need to "install" it.

Okay. I will try this. But where can I get the newest version? I found
this two pages:

http://www.loveruby.net/archive/

http://cvs.rose.radionet.fi/cgi-bin/viewcvs.cgi/roseroot/extras/ruby/lib/
(http://cvs.rose.radionet.fi/cgi-bin...root/extras/ruby/lib/fileutils.rb?rev=1.1.1.1)

greetings
Dirk Einecke
 
D

Dirk Einecke

Hi Mark.

Mark said:
Go to the official CVS repository to get the latest version.

http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/lib/fileutils.rb

.. and click download. Then just stick it in the same directory as the
other files, and viola! :)

Thanks.
But one thing is strange:

Working fine:
http://cvs.rose.radionet.fi/cgi-bin...root/extras/ruby/lib/fileutils.rb?rev=1.1.1.1

Does not work:
http://www.ruby-lang.org/cgi-bin/cv...fileutils.rb?rev=1.41;content-type=text/plain

Okay - well that's one thing for the next day. Now I will go to sleep
(it's 00:48am here in germany) :)

greetings
Dirk Einecke
 
K

Kristof Bastiaensen

Hi.

I would like to delete a directory. In this directory are other
directories an files. So I have to delete this subdirectories an files
at first. Now I search a good solution to solve this problem. My first
idea was to look in the given directory and if there are subdirectories
to look in this directories... Is this the right way? Is there a ready
function for this little problem? Or has anybody written a function for
it?

greetings
Dirk Einecke

`rm -rf directory`
:)
 
R

Robert Klemme

Kristof Bastiaensen said:
`rm -rf directory`
:)

Another one liner would be:

Dir[File.join(dir, '**', '*')].reverse.each {|f| (File.directory? ? Dir :
File).delete f}
:)

Note: this one is portable also.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top