in reply to Automatically creating incremental file names
#check for dup name if (-e "$fn_out/$fname") { print "Dup:: $fn_out/$fname\n"; my ($name, $ext) = ($fname =~ /^(.+?)(\.[^.]+)?$/); $ext = "" unless $ext; my $i = 0; while (-e "$fn_out/$fname_new") { ++ $i; $fname_new = sprintf "$name-%02d$ext", $i; } my $uni = $fname_new; print "New:: $fn_out/$uni\n\n"; rename ("$fn_in/$fname", "$fn_out/$uni"); }else{ rename ("$fn_in/$fname", "$fn_out/$fname"); print "Dest:: $fn_out/$fname\n\n"; }
$fn_in is the directory for the in bucket of file names. $fn_out is the directory for the out bucket of file names
The critical line is a self incrementing line until it finds a file name that does not exist in the out bucket.
$fname_new = sprintf "$name-%02d$ext", $i;
The format of the file name will be FILENAME-NN.EXT where NN is an incrementing number. You can easily customize this to your likes.
I like using this solution because it works on all platorms and does not require any overhead or modules.
Richard
There are three types of people in this world, those that can count and those that cannot. Anon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Automatically creating incremental file names
by graff (Chancellor) on May 28, 2003 at 07:01 UTC |