All pastes #827594 Raw Edit

remove_hidden.pl

public text v1 · immutable
#827594 ·published 2007-12-21 21:38 UTC
rendered paste body
use strict;

# # # # # # # # # # # # # # # # #
# Find Files Prefixed With This #
# # # # # # # # # # # # # # # # #

my $prefix = '_';

# # # # # # #
# The Start #
# # # # # # #

chdir($ARGV[0]) or do {
  print "Cannot enter directory... Enter full path, or nothing to use current directory: ";
  chomp(my $path = <STDIN>);
  $path = './' unless $path;
  chdir($path) or do {
    print "Couldn't enter current or given directory...";
    exit 1; # throw error (win32?)
  };
};

opendir(PARENT,'./');
my @parent = readdir(PARENT);
closedir(PARENT);
recurse_clean(@parent);

# # # # # # #
# The Heart #
# # # # # # #

sub recurse_clean {
  my @listing = @_;

  foreach my $path (@listing) {
    next if $path =~ /^\.\.?$/; # skip '.' and '..'

    print "Testing $path..... ";
    chdir($path) or do {
      print "File.\nChecking $path..... ";
      if ($path =~ /^$prefix.+/) {
        print "We have a match!\nRemoving $path..... ";
        unlink($path) or do {
          print "Failed, moving on.\n";
          next;
        };
        print "Done, proceeding.\n";
      } else {
        print "No match.\n"
      }
      next;
    };

    print "Directory.\nReading $path..... ";
    opendir(CHILD,'./') or do {
      print "Failed, moving on.\n";
      next;
    };
    my @child = readdir(CHILD);
    closedir(CHILD);
    recurse_clean(@child);
    chdir('..');
  }
}

# # # # # #
# The End #
# # # # # #

print "Done! For better or worse. Press enter to quit.\n";
<STDIN>;