Delete a directory with all sub-directories an files

Discussion in 'Ruby' started by Dirk Einecke, Apr 17, 2004.

  1. Dirk Einecke

    Dirk Einecke Guest

    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
     
    Dirk Einecke, Apr 17, 2004
    #1
    1. Advertising

  2. On Sat, 17 Apr 2004 21:53:48 +0200, Dirk Einecke wrote:
    > 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
     
    Simon Strandgaard, Apr 17, 2004
    #2
    1. Advertising

  3. Dirk Einecke

    Dirk Einecke Guest

    Hi Simon.

    Simon Strandgaard wrote:
    >>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")


    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
     
    Dirk Einecke, Apr 17, 2004
    #3
  4. On Sat, 2004-04-17 at 14:44, Dirk Einecke wrote:

    > > 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?


    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
     
    Michael C. Libby, Apr 17, 2004
    #4
  5. Dirk Einecke

    Dirk Einecke Guest

    Hi Michael.

    Michael C. Libby wrote:
    > 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
     
    Dirk Einecke, Apr 17, 2004
    #5
  6. Dirk Einecke

    Mark Hubbart Guest

    On Apr 17, 2004, at 1:54 PM, Dirk Einecke wrote:

    >> 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?


    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! :)

    cheers,
    --Mark
     
    Mark Hubbart, Apr 17, 2004
    #6
  7. Dirk Einecke

    Dirk Einecke Guest

    Hi Mark.

    Mark Hubbart wrote:
    > 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
     
    Dirk Einecke, Apr 17, 2004
    #7
  8. On Sat, 17 Apr 2004 20:53:48 +0200, Dirk Einecke wrote:

    > 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`
    :)
     
    Kristof Bastiaensen, Apr 18, 2004
    #8
  9. "Kristof Bastiaensen" <> schrieb im Newsbeitrag
    news:4082752a$0$973$...
    > On Sat, 17 Apr 2004 20:53:48 +0200, Dirk Einecke wrote:
    >
    > > 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`
    > :)


    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
     
    Robert Klemme, Apr 19, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    494
    Joel Finkel
    Sep 12, 2003
  2. TJS
    Replies:
    1
    Views:
    519
    Eliyahu Goldin
    Jun 23, 2004
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,062
    Lawrence D'Oliveiro
    May 20, 2011
  4. Adam Petrie
    Replies:
    8
    Views:
    309
    Adam Petrie
    Oct 11, 2004
  5. Ron Smith
    Replies:
    5
    Views:
    151
    Michele Dondi
    Nov 2, 2004
Loading...

Share This Page