Problems and suggestions:
- Simplify your grep by using the /i flag to make it case-insensitive.
- Since the foreach loop puts each filename in $_, $ucount is completely superfluous and likely to confuse things.
- You put a matched filename in $frameu and then....do nothing with it.
- You print the filenames that don't match.
- You increment the counter, but can you be certain the counter will always correspond to the number in the filename?
- I think what you want is to find the largest-numbered file in the directory, and then print the next-largest number.
So, rewriting with those things in mind:
#!/usr/bin/perl
use strict;
use warnings;
my $topdir = 'D:/PGN/ELSEVIER/FNS/1(1)/press';
opendir HU, $topdir; # don't quote a single scalar
my @files = grep /\.txt$/i, readdir HU; # will also match .TXT
closedir HU;
my $biggest = 0; #keep track of largest number found
for my $frame (@files){
if($frame =~ m|^partial_pressps_report(\d+)|){
my $n = $1; # capture number from filename
if( $n > $biggest ){ # is it bigger than $biggest?
$biggest = $n; # then save it
}
}
}
$biggest++; # got the largest one, now increment it
print $biggest; # and print it
|