################################################################################ # # # ############################################################################ # # # Title : remove_hidden.pl # # # # Description : Checks a directory for hidden files, based on a regex. It # # # # unlinks all matches with togglable confirmation. # # # # Coded by : Justin Lee # # # # Version : 0.0.1-RC2 # # # # Date : Friday, Ferubary 22, 2008 # # # # License : GNU General Public License v.2 # # # # Copyright (C) 2008 Justin Lee # # # ############################################################################ # # # # ############################################################################ # # # This program is free software; you can redistribute it and/or # # # # modify it under the terms of the GNU General Public License version 2 # # # # as published by the Free Software Foundation. # # # # # # # # This program is distributed in the hope that it will be useful, # # # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # # # GNU General Public License for more details. # # # # # # # # You should have received a copy of the GNU General Public License # # # # along with this program; if not, write to the Free Software # # # # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # # # # 02110-1301, USA. Or, see: # # # ############################################################################ # # # ################################################################################ use strict; use warnings; use Getopt::Std; use File::Basename; use File::Find; use Term::ReadKey; use constant {NAME => basename($0) . ' > '}; # # # # # # # # # # # Deletion Regex! # # # # # # # # # # # my %delete = ( mac => '^\.(?:ds_store|.*\.m(?:4a|p3))$', win => '^(?:thumbs\.db|desktop\.ini)$', all => '^(?:\.(?:ds_store|.*\.m(?:4a|p3))|thumbs\.db|desktop\.ini)$' ); # # # # # # # # The Start # # # # # # # # my %opt; my $regex; my $key; getopts('hCmawc:d:',\%opt) or die 'Improper command line usage! See `' . basename($0) . " -h' for help.\n"; help() if $opt{h}; print NAME, "Welcome to the hidden file remover! (by Justin)\n"; # ouch, my logic if (defined $opt{m}) { $regex = $delete{'mac'}; print NAME, "Using Mac deletion filter!\n"; } elsif (defined $opt{w}) { $regex = $delete{'win'}; print NAME, "Using Win32 deletion filter!\n"; } elsif (defined $opt{a}) { $regex = $delete{'all'}; print NAME, "Using both Win32 and Mac deletion filter!\n"; } elsif (defined $opt{c}) { $regex = $opt{c}; print NAME, "Is `$regex' what you wanted? [y/n]\n"; undef $key; ReadMode 4; $key = ReadKey(0); ReadMode 0; $regex = get_gex() unless $key =~ /y/io; } else { print NAME, "No deletion filter specified!\n"; print NAME, "Would you like to select one? [y/n]\n"; undef $key; ReadMode 4; $key = ReadKey(0); ReadMode 0; unless ($key =~ /y/io) { print NAME, "Okay! See yah later!\n"; exit 0; } print NAME, "\nPlease select a mode to use:\n"; map { print ' ' x int(length(NAME) / 6), " $_->[0]. \u$_->[1] mode: \u$_->[2].\n"; } ( [1,'mac','removes `.ds_store\' and `..mp3\' files'], [2,'win','removes `thumbs.db\' and `desktop.ini\' files'], [3,'all','removes all of the above'], [4,'custom','you input a deletion filter in the style of a regex'], ); print NAME, "Please press 1, 2, 3, or 4.\n"; print NAME, "Any other key will quit.\n"; undef $key; ReadMode 4; $key = ReadKey(0); ReadMode 0; unless ($key =~ /^[1-4]$/o) { print "Okay! See yah later!\n"; exit 0; } ReadMode 0; if ($key == 1) { $regex = $delete{'mac'}; print NAME, "Using Mac deletion filter!\n"; } elsif ($key == 2) { $regex = $delete{'win'}; print NAME, "Using Win32 deletion filter!\n"; } elsif ($key == 3) { $regex = $delete{'all'}; print NAME, "Using both Win32 and Mac deletion filter!\n"; } elsif ($key == 4) { $regex = get_gex(); } } # clean up ReadMode 0; undef $key; unless (defined $opt{d}) { die 'No directory specified! See `' . basename($0) . " -h' for help.\n"; } else { chdir($opt{d}) or die "Unable to access `$opt{d}', did you specify a valid path?\n($!)\n"; } find( # subroutine to parse find \&parsefind, # look in the directory we just chdir'd into '.' ); exit 0; sub parsefind { my $cpath = $_; return if -d $_; if ($_ =~ /$regex/oi) { if (defined $opt{C}) { print NAME, "Unlinking `$_', continue? [y/n]\n"; undef $key; ReadMode 4; $key = ReadKey(0); ReadMode 0; return unless $key =~ /y/io; } print NAME, "Deleting `$_'....."; unless(unlink($_)) { print "Failed.\n"; return; } print "Done!\n"; return; } } sub get_gex { while (1) { print NAME, "Please input a deletion filter.\n"; print NAME, "Input nothing and press return to quit.\n"; print NAME, "Perl regex: "; chomp(my $regex = ); unless(length $regex) { print NAME, "Okay! See yah later!\n"; exit 0; } print NAME, "Is `$regex' what you wanted? [y/n]\n"; undef my $key; ReadMode 4; $key = ReadKey(0); ReadMode 0; redo unless $key =~ /y/io; print NAME, "Okay, `$regex' it is!\n"; return $regex; } } sub help { # hmawc:d: print <<"EOH"; Name : Remove Hidden Description : Removes hidden files based on perl regex. Author : Justin Lee < kool.name at gmail.com > Usuage : @{[ basename($0) ]} [options] Options : -h Print this message. -C Ask for confirmation before deleting! -m Use Mac deletion filter: Looks for `.DS_Store' and `..mp3' files. -w Use Win32 deletion filter: Looks for `Thumbs.db' and `desktop.ini' files. -a Use both Mac and Win32 filters. -c < valid perl regex > Specify a custom deletion filter. It will ask for confirmation before running. -d < X:\\path\\to\\folder to\\clean_up\\ > (Required) Specifies the path to the folder that will be cleaned up. EOH exit 0; }