in reply to Rmtree with exclude

use strict; use warnings; use File::Slurp; use File::Path qw( remove_tree ); use Cwd; my $dir = shift; #path/to/cache my @dirs = read_dir( $dir ); my $path = getcwd( $dir ); for (@dirs) { next if ( -f $_ ); if ( $_ !~ /(?:_)/ ) { my $removed = $path . "$dir/$_"; remove_tree($removed); print "removed : $removed\n"; } }
I used Perl Regex Tester to create a regex that matches _ in the path, if found then it uses rmtree to remove it. This is completely untested as i am getting ready for work!

EDIT: Changed =~ to !~ so it will match all dirs that dont have '_' in it.
EDIT2: Just made the code so it should work as intended.

Replies are listed 'Best First'.
Re^2: Rmtree with exclude
by Mery84 (Novice) on Dec 02, 2014 at 12:05 UTC
    Thank you James!
    Your code after few adjustments works like a charm.
    #!/usr/bin/perl use strict; use warnings; use File::Slurp; use File::Path qw( remove_tree ); my $dir = "/opt/ssi/cache"; my $err_list = "/home/az/log.txt"; my @dirs = read_dir( $dir ); for (@dirs) { next if ( -f $_ ); if ( $_ !~ /(?:_)/ ) { my $removed = "$dir/$_"; remove_tree( $removed , { verbose => 1, error => \my $err_list, }); print "removed : $removed\n"; } }
      Awesome man, glad i could help. Though i am sure any of the other examples would have did the trick as well ;)

      Also, you should be very careful with remove_tree, and if possible add in absolute path to folder that you want to delete, that way the path that gets fed to remove tree is the exact folder you want deleted.

      Upon revising this, I deleted a folder in the scripts dir that had the same name of a dir in the test dir (was my mistake for using the same folder name i guess). needless to say it had abunch of my perl scripts in it and now i am having to try to use recovery software to hopefully get them back.