http://www.perlmonks.org?node_id=29734

Adam has asked for the wisdom of the Perl Monks concerning the following question: (directories)

On an NT machine I managed to do it with:
`rd /s /q "$_"` for grep {-d} glob '*';
(That deletes all the sub directories (and their contents) in the current dir.) But I was wondering if there is a "Perl" way.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I delete a directory without emptying it first?
by DracoWulf (Initiate) on Sep 12, 2000 at 01:19 UTC
    The simplest cross-platform method I have found was with the File::Path function rmtree. You pass it an array of directories you want to delete. It will delete the entire tree.
    use File::Path; rmtree([ '/tmp/foo', '/tmp/bar' ]);
Re: How do I delete a directory without emptying it first?
by gav^ (Curate) on Jan 17, 2002 at 07:04 UTC
    I'm not sure if this is a better method than using File::Path, but it is a hell of a lot better than rolling your own with File::Find :)
    use File::Remove qw(remove); remove \1, "dir";

    gav^

Re: How do I delete a directory without emptying it first?
by swngnmonk (Pilgrim) on Mar 09, 2004 at 17:50 UTC
    What about rmtree?
    use File::Path qw(rmtree); rmtree($dir);
    File::Path and File::Path::Functions are great to use when you're looking for perl solutions to file management, and want to avoid system-specific issues.

    Originally posted as a Categorized Answer.

Re: How do I delete a directory without emptying it first?
by ZZamboni (Curate) on Aug 26, 2000 at 02:36 UTC
    The question is: what would happen to the files in that directory after you delete it? Moved? Deleted? That's why most operating systems don't allow you to remove a directory that has any files in it. The operating system cannot guess what you want done with the files. You have to do something with them first.
Re: How do I delete a directory without emptying it first?
by cLive ;-) (Prior) on Apr 02, 2001 at 08:59 UTC
    *nix/linux?
    my $dir = '/directory/path'; system('rm','-r',$dir);
    And if you're doing this from input data... well if you are, you're probably mad :)

    cLive ;-)

Re: How do I delete a directory without emptying it first?
by trala (Initiate) on Jan 17, 2002 at 05:07 UTC
    Okay, just came up with this for a script I am working on. If this is really stupid, please someone let me know!
    use File::Find; finddepth (\&remove_dir, "$directory"); rmdir ( "$directory" ) or die ("Could not remove $directory"); sub remove_dir { # for a directory, this will be 0 if ( ! (stat("$File::Find::name"))[7] ) { rmdir("$File::Find::name"); } else { unlink("$File::Find::name"); } }
    In theory, finddepth will find the deepest items first (files inside the directories). Once the files are unlinked, the directories will be empty and can be removed.
      To make your example a bit shorter you could go for:
      use File::Find; finddepth( sub { (-d) ? rmdir : unlink }, $directory);

      But I would recommend using File::Remove as per my example.

      gav^

Re: How do I delete a directory without emptying it first?
by Anonymous Monk on Mar 09, 2004 at 16:04 UTC
    rm -ri directory before use this command, please man rm first.

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.