use File::CounterFile; my $counter = File::CounterFile->new("/path/to/sentinel.txt", "0000"); chdir "/path/to/where/the/files/are" or die "chdir: $!"; ## see if a new file has arrived while (-e (my $file = "elmo$counter.txt")) { process_file($file); $counter = "0000" if ++$counter == "10000"; }