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

Hi Monks!

I am trying to delete a directory inside another directory, but my code is not working, I just can't see it. Could someone show me where the problem is here:

#!/perl/bin/perl -w use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use File::Find; print header(); my $dir_rv = "c:/progra~1/apache~1/apache2/cgi-bin/"."del"; opendir DIR, $dir_rv or return; my @contents = map "$dir_rv/$_",sort grep !/^\.\.?$/,readdir DIR +; closedir DIR; foreach (@contents) { next unless !-l && -d; print "<br><font color=red>Found:^^$_^^</font><br>"; rmdir ( "$_" ) or die ("Could not remove $_"); print "<br><font color=red>Found:^^$_^^</font><br>"; }


It should find any directory(ies) inside of the specific directory "del" and get rid of them, but it isn't. It find them right, but the delete part doesn't work! Thanks for the Help!!!

Replies are listed 'Best First'.
Re: Deleting a Directory
by ropey (Hermit) on Nov 16, 2006 at 18:45 UTC
    If you changed
    rmdir ( "$_" ) or die ("Could not remove $_");
    to
    rmdir ( "$_" ) or die "Could not remove $_ $!";
    Then a more meaningful message would appear, which may well help you solve your issue.
Re: Deleting a Directory
by chargrill (Parson) on Nov 16, 2006 at 18:52 UTC

    Are the directories empty?

    $ perldoc -f rmdir rmdir FILENAME rmdir Deletes the directory specified by FILENAME if that directory is empty. If it succeeds it returns true, otherwise it returns false and sets "$!" (errno). If FILENAME is omitted, uses "$_".

    Maybe you need to go through the directories and delete any files in them.



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
      No that's one of my problems now, some directories will have some useless files in it.
Re: Deleting a Directory
by ahmad (Hermit) on Nov 16, 2006 at 19:24 UTC

    Hello ,

    it might be not working properly because there are some files inside the directory you want to delete

    you'll need to use rmtree function inside File::Path instead of using rmdir built-in function

    NOTE : take my advice only , and ONLY if you dont need the files inside the directory you want to delete

Re: Deleting a Directory
by jdhedden (Deacon) on Nov 17, 2006 at 15:37 UTC
    Dear Anonymous Monk,
    Much wisdom on basic subjects such as this can be found in the [id://Categorized Questions and Answers]. There you will find answers for your specific question: How do I delete a directory without emptying it first?

    Remember: There's always one more bug.
Re: Deleting a Directory
by davorg (Chancellor) on Nov 17, 2006 at 15:46 UTC