In article <2FB59B145095D511A7C90050BAC349F31730A3@MAIL>, John Edwards wrote:
> 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.
Here's a possible way to do it:
#!/usr/bin/perl -w
use strict;
myRename('Sherlock(1).all');
sub myRename {
my $filename = shift;
warn "Can't find '$filename'\n" and return unless -e $filename;
my $newname;
if ( $filename =~ m! (.*) \( (\d+) \) \. ([^.]+) $!x ){
---- ------------ - ------- _
| | | |
|
filename number in () dot extension eol
$newname = "$1(" . ($2+1) . ").$3";
}
else {
($newname = $filename) =~ s/\.([^.]+)$/(1).$1/;
}
rename ( $filename, $newname )
or warn "Could not rename '$filename' to '$newname'\n" and next;
print "$filename => $newname\n";
}
__END__
--
briac
<< dynamic .sig on strike, we apologize for the inconvenience >>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]