Delfer has asked for the wisdom of the Perl Monks concerning the following question:

I want to remove a directory and there are several files and subdirs in it? rmdir()can not do it.expect your reply.

Replies are listed 'Best First'.
Re: How can I remove a Dir?
by btrott (Parson) on Feb 18, 2000 at 22:40 UTC
    Use File::Find.
    #!/usr/local/bin/perl -w use strict; use File::Find; my $dir = "/foo/bar"; # the directory you want removed sub remove { if (-d $_) { rmdir $File::Find::name; } else { unlink $File::Find::name; } } finddepth(\&remove, $dir); rmdir $dir;
    There's probably a better way of doing it, but this should work.
Re: How can I remove a Dir?
by plaid (Chaplain) on Feb 21, 2000 at 03:29 UTC
    use File::Path; my $dir = '/to_remove'; rmtree([$dir], 0, 0);
    File::Path comes with the standard distribution.
      Let's try that again, now that I've actually tried reading something..
      use File::Path; my $dir = '/to_remove'; rmpath([$dir], 0, 0);
      File::Path comes with the standard distribution
        In fact, it's rmtree() instead rmpath() (at least in perl 5) 8^)

        Jose Quesada