#!/usr/bin/perl use strict; use warnings; use 5.010; use Cwd; use File::Spec; use Getopt::Long; use File::Find::Rule; $| = 1; my $PROGNAME = ( split m|[\\/]|, $0 )[-1]; help() if !@ARGV; my ( @filemask, @dir ); my ( $verbose, $notest, $progressbar, $debug, $force, $help ); GetOptions( "f|file=s" => \@filemask, # filemasks to select several files (DEFAULT: all files) "d|dir=s" => \@dir, # dirs for scan (DEFAULT: current dir) "progress" => \$progressbar, # show progress (DEFAULT: on) "notest" => \$notest, # force to run in real mode (DEFAULT: off) "force" => \$force, # force confirm with Yes all actions (DEFAULT: off) "v|verbose" => \$verbose, # show information about current action (DEFAULT: off) "debug" => \$debug, # show debug info "h|help" => \$help # show help ) or die "Error in command line arguments\n"; ### set defaults of parameters if not already set push @dir, Cwd::cwd() if not @dir; # use current dir, if no parameter --dir defined $verbose //= 0; $notest //= 0; $force //= 0; $progressbar //= 1; $progressbar = 0 if $verbose; ### # my $testmode = not $notest; if ($debug) { say '--------------------'; say "Testmode: $testmode"; say "Force: $force"; say "Progressbar: $progressbar"; say "Verbose: $verbose"; say "Dirs: @dir"; say "Masks: @filemask"; say '--------------------'; exit 4711; } help() if $help; if (not @filemask) { say "!!!! ##ERROR## !!!! filemask must not be empty!\n"; help(); } say "Testmode #####################" if $testmode; #### search files my $rule = File::Find::Rule->new(); my $progress_count = 0; $rule->name(@filemask); # set filemasks for search $rule->exec( # this sub is called for each matched file of search sub { my ( $shortfn, $dir, $filename ) = @_; $filename = File::Spec->rel2abs($filename); $progress_count++; if ($progressbar) { print '.' if $progress_count % 100 == 0; # show progress all 100 files } elsif ( $verbose and $testmode ) { print '#TESTMODE# found '; say $filename; # print filename for testing, no changing of files } if ( not $testmode ) { # is no test, let the program run in real life my $char = 'Y'; if ( not $force ) { # interactive confirm $char = 'N'; print "Confirm action for $filename. Really? (y/N) "; $char = getc(); getc(); # fetch Enter after y or n } if ( uc $char eq 'Y' ) { # Y means let run action for this file action_for_file($filename); } } } ); $rule->in(@dir); #start search in dirs now exit; # ----------------------------------------------------------------------------- sub action_for_file { my $filename = shift; my $result; say "Processing $filename" if $verbose; $result = ######################################################################## ###### !!!!! DO THE VERY BAD ACTION FOR THE FILE HERE!!!!! ######################################################################## unlink($filename) # !!!!!!!! BEWARE !!!!!!!! ######################################################################## ######################################################################## ; return $result; } # ------------------------------------ sub help { print <<"HELP"; Usage: $PROGNAME --file=FILEMASK --dir=DIR1 Parameters: --dir Selected directory --file Filemask for selecting several files --progress Action with progressbar --verbose Show some information about current actions --force Force confirm for all actions with Yes --notest Force action for files into real mode (test mode is off!) --help This help --debug Some debugging information for program Example: $PROGNAME --file=a.[12]* -d=X:\\TEST --verbose Processes all files a.1* .. a.2* from dir X:\\TEST in testmode $PROGNAME --file=a.[12]* -d=/home/useer998/TEST --verbose Processes all files a.1* .. a.2* from dir /home/useer998/TEST in testmode $PROGNAME --file=a.[2]* -d=. --notest Processes all files a.2* from current dir in real mode (ACTIONS ARE PROCESSED!) with confirmation $PROGNAME --file=a.[2]* --notest --force Processes all files a.2* with no confirmation in real mode (ACTIONS ARE PROCESSED!) HELP exit 4711; } __END__