This is another approach. Read the file names in the output directory, filter out the ones that aren't really simple files (maybe there is some directory with a similar name), get the revision numbers of those files, get the max revision number and then add one to that.
#!/usr/bin/perl -w
use strict;
use List::Util qw(max); # List::Util is a core module
# that means you don't have to
# install it - its already there.
my $output_dir = 'C:/TEMP';
my $program_root_name = "some_prog";
opendir DIR, $output_dir
or die "cannot open $output_dir for reading $!";
my @current_numbers =
map { /^$program_root_name(\d+)$/ ? $1 : () } # just the numbers
grep{ -f "$output_dir/$_"}readdir DIR; # only "real" files
# not directories
closedir DIR; # purely optional
foreach (@current_numbers) #intermediate print routine...
{
print "$output_dir/$program_root_name$_ exists!\n";
}
my $max_cur_number = max(@current_numbers);
my $new_max = ++$max_cur_number;
print "The next highest number is $new_max\n";
print "Full name of next file in sequence would be: ".
"$output_dir/$program_root_name$new_max\n";
__END__
The above prints:
Note: I made the dummy files: some_prog1 and some_prog3...
C:/TEMP/some_prog1 exists!
C:/TEMP/some_prog3 exists!
The next highest number is 4
Full name of next file in sequence would be: C:/TEMP/some_prog4
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link or
or How to display code and escape characters
are good places to start.
|