Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: incrementing the file

by Marshall (Canon)
on Feb 22, 2012 at 07:19 UTC ( #955462=note: print w/replies, xml ) Need Help??

in reply to incrementing the file

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://955462]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2022-08-16 14:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found