John Edwards wrote: > > Hi group. Hello,
> I have the following snippet of code. It's not working and I've been going > round in circles trying to figure out why. > > I need a routine that will look at the filename, if that filename already > exists, then add a (1) to the end. I've got the checking for existance > sorted, it's the generation of the new file name that is the issue. > > E.g. test.txt exists so create test(1).txt > > If test(1).txt exists then create test(2).txt and so on > > Here is the problematic code I have so far > > -- code -- > my ($name, $ext) = split(/\./,$fileoutname); > > if ($name =~ /\((\d{1,1})\)$/) { # Looks for (1) on the end for example > my $number = $1; > $number++; > $name =~ tr/\(\d\)/\($number\)/; > } else { > $name .= "(1)"; > } > > $fileoutname = "$name\.$ext"; > > -- end code -- > > There's probally some really basic errors in there, and maybe a much better > way of doing it... This should do what you want: use File::Basename; my ( $name, $path, $ext ) = fileparse( $fileoutname, '\..*' ); while ( -e "$path$name$ext" ) { $name .= '(1)' unless $name =~ s/\((\d+)\)$/'('.($1+1).')'/e; } $fileoutname = "$path$name$ext"; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]