NodeReaper has asked for the wisdom of the Perl Monks concerning the following question:

This node was taken out by the NodeReaper on Nov 09, 2011 at 11:06 UTC
  • Comment on Reaped: incrementing the last file name

Replies are listed 'Best First'.
Re: incrementing the last file name
by aaron_baugher (Curate) on Nov 09, 2011 at 11:26 UTC

    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

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

Re: incrementing the last file name
by ansh batra (Friar) on Nov 09, 2011 at 08:42 UTC
    dulicate thread!!!
    refer this

      Hi Monk

      the last file name have to be incremented from the list pls refer the increment the last file name thread

      Thanks, Ahmed