Using your code: my $digit_width = 4; # This isnt necessarily true every time my $min = 1; my $max = 12; my $prefix = "test_"; my $ext = ".txt"; while ( my $file = readdir DIR ) { next unless ( $file =~ /^ $prefix (\d{$digit_width}) $ext $/x ); if ( $1 >= $min and $1 <= $max ) { # we have a match... now now we want to create the new file name # Whatever $SetName was so if $SetName = "Chapter \1" which means # The new file name for the program to modify in the directory is # to "Chapter 01.txt" for the 1st chapter file which wouldve been # Test_001.txt in the directory it searched in. I will then # have the code rename the file. } }