How to use perl rmtree tree to remove a directory if it contains a certain file? -
i trying remove directory's on given path, if contain file.
#!/usr/bin/perl use strict; use file::find; use file::path qw( rmtree ); find(\&rm_errors, $_) @argv; sub rm_errors{ if ($_ eq "git_errors.txt"){ $path = $file::find::dir; rmtree( $path ); } } finding directory's contain files working but, rmtree not deleting directory. can tell me why?
for immediate question asking, way find out problem is use diagnostic die statement:
rmtree($path) or die "cannot rmtree '$path' : $!"; that way, learn error system reports.
however, there issue well. affecting file system while iterating through it, not excellent idea. perhaps better:
my @dirs; find(sub { push @dirs, $file::find::dir if $_ eq "git_errors.txt" }, $_) @argv; $path (@dirs) { rmtree($path) or die "cannot rmtree '$path' : $!"; } which say, find directories first, delete them.
Comments
Post a Comment