How to use perl rmtree tree to remove a directory if it contains a certain file?
perl
Solution
For the immediate question that you are asking, the way to find out what the problem is is to use a diagnostic `die` statement:
rmtree($path) or die "Cannot rmtree '$path' : $!";
That way, you will learn what error the system reports.
However, there is another issue as well. You are affecting the file system while iterating through it, which is not an excellent idea. Something like this would perhaps be better:
my @dirs;
find(sub { push @dirs, $File::Find::dir if $_ eq "git_errors.txt" }, $_) for @ARGV;
for my $path (@dirs) {
rmtree($path) or die "Cannot rmtree '$path' : $!";
}
Which is to say, find the directories first, then delete them.
Problem
i am trying to remove all directory's on a given a path, if they contain a certain file. ``` #!/usr/bin/perl use strict; use File::Find; use File::Path qw( rmtree ); find(\&rm_errors, $_) for @ARGV; sub rm_errors{ if ($_ eq "git_errors.txt"){ my $path = $File::Find::dir; rmtree( $path ); } } ``` Finding the directory's that contain the files is working but, rmtree is not deleting the directory. Can anyone tell me why?