$check ="ABCD" # this value will come from an array # ... $var = substr($file_name, 0, 3); # ... if($var eq $check){ #### sub scandir { my $dir=shift; my $test=join '|', map quotemeta, @_; chdir $dir; for (glob '*') { next unless /^($test)(.*)/; my $new="$1D$2"; print "`$_' => `$new'\n"; if (-e $new) { warn "Warning: `$new' exists\n"; # or do something more complex... next; } else { rename $_, $new or die "Can't rename to `$new': $!\n"; } } }