file renamer... request feedback
file renamer... request feedback
am 25.03.2006 05:52:02 von avidfan
#!/usr/bin/perl
use strict;
use File::Find;
use warnings;
use diagnostics;
finddepth(\&fixnames, ".");
sub fixnames
{
return if /^\./;
return if /lost\+found/;
my $new = $_;
my $old = $_;
$new = lc $new ;
$new =~ tr/a-z0-9._/_/c;
$new =~ s/^_//g;
$new =~ s/\.mpeg$/mpg/g;
$new =~ s/\.ram$/rm/g;
$new =~ s/\.qt$/mov/g;
$new =~ s/\.jpeg$/jpg/g;
$new =~ s/_\././g;
$new =~ s/\._/_/g;
$new =~ tr/__/_/s;
$new =~ tr/.././s;
return if $new eq $old;
# rename($old, $new) or die $!;
print "renaming \n $File::Find::name ---> \n $File::Find::dir/$new\n" or
die $!;
Re: file renamer... request feedback
am 25.03.2006 07:44:05 von someone
TOC wrote:
> #!/usr/bin/perl
> use strict;
> use File::Find;
> use warnings;
> use diagnostics;
>
> finddepth(\&fixnames, ".");
>
> sub fixnames
> {
> return if /^\./;
> return if /lost\+found/;
> my $new = $_;
> my $old = $_;
> $new = lc $new ;
my $new = lc;
my $old = $_;
> $new =~ tr/a-z0-9._/_/c;
> $new =~ s/^_//g;
> $new =~ s/\.mpeg$/mpg/g;
> $new =~ s/\.ram$/rm/g;
> $new =~ s/\.qt$/mov/g;
> $new =~ s/\.jpeg$/jpg/g;
All the previous substitutions are anchored so the /g option is superfluous.
> $new =~ s/_\././g;
> $new =~ s/\._/_/g;
> $new =~ tr/__/_/s;
> $new =~ tr/.././s;
Or simply:
$new =~ tr/_.//s;
Also, if you want to reduce some typing you could use a for loop:
for ( $new ) {
tr/a-z0-9._/_/c;
s/^_//;
s/\.mpeg$/mpg/;
s/\.ram$/rm/;
s/\.qt$/mov/;
s/\.jpeg$/jpg/;
s/_\././g;
s/\._/_/g;
tr/_.//s;
}
> return if $new eq $old;
You should probably also return if the file name $new already exists:
return if $new eq $old or -e $new;
> # rename($old, $new) or die $!;
> print "renaming \n $File::Find::name ---> \n $File::Find::dir/$new\n" or
> die $!;
Missing ending }
John
--
use Perl;
program
fulfillment
Re: file renamer... request feedback
am 25.03.2006 08:14:05 von avidfan
"John W. Krahn" wrote in news:VE5Vf.3542$Ph4.1234
@edtnps90:
> return if $new eq $old or -e $new;
thanks, if you see anything else, let me know...
#!/usr/bin/perl
use strict;
use File::Find;
use warnings;
finddepth(\&fixnames, ".");
sub fixnames
{
return if /^\./;
return if /lost\+found/;
my $o = $_ ;
for ( $_ )
{
y/A-Z/a-z/;
y/a-z0-9._/_/c;
s/^_+//;
s/\.mpeg$/\.mpg/;
s/\.ram$/\.rm/;
s/\.qt$/\.mov/;
s/\.jpeg$/\.jpg/;
s/_\./\./g;
s/\._/_/g;
y/_//s;
y/.//s;
}
return if $_ eq $o or -e $_;
# rename $o,$_ or warn "Cannot rename $o:$!\n";
print "renaming \n $File::Find::name ---> \n $File::Find::dir/$_\n";
Re: file renamer... request feedback
am 25.03.2006 08:37:32 von someone
TOC wrote:
> "John W. Krahn" wrote in news:VE5Vf.3542$Ph4.1234
> @edtnps90:
>
>>return if $new eq $old or -e $new;
>
> thanks, if you see anything else, let me know...
>
>
>
> #!/usr/bin/perl
> use strict;
> use File::Find;
> use warnings;
>
> finddepth(\&fixnames, ".");
>
> sub fixnames
> {
> return if /^\./;
> return if /lost\+found/;
> my $o = $_ ;
> for ( $_ )
> {
> y/A-Z/a-z/;
> y/a-z0-9._/_/c;
> s/^_+//;
> s/\.mpeg$/\.mpg/;
> s/\.ram$/\.rm/;
> s/\.qt$/\.mov/;
> s/\.jpeg$/\.jpg/;
> s/_\./\./g;
> s/\._/_/g;
> y/_//s;
> y/.//s;
> }
> return if $_ eq $o or -e $_;
> # rename $o,$_ or warn "Cannot rename $o:$!\n";
> print "renaming \n $File::Find::name ---> \n $File::Find::dir/$_\n";
You don't need the for loop because $_ is the default variable for
substitution and transliteration so:
sub fixnames
{
return if /^\./ or /lost\+found/;
my $o = $_ ;
y/A-Z/a-z/;
y/a-z0-9._/_/c;
s/^_+//;
s/\.mpeg$/\.mpg/;
s/\.ram$/\.rm/;
s/\.qt$/\.mov/;
s/\.jpeg$/\.jpg/;
s/_\./\./g;
s/\._/_/g;
y/_.//s;
return if $_ eq $o or -e;
# rename $o,$_ or warn "Cannot rename $o:$!\n";
print "renaming \n $File::Find::dir/$o ---> \n $File::Find::dir/$_\n";
}
John
--
use Perl;
program
fulfillment
Re: file renamer... request feedback
am 25.03.2006 09:08:25 von avidfan
"John W. Krahn" wrote in news:0r6Vf.5531$K11.1935
@clgrps12:
> You don't need the for loop because $_ is the default variable for
> substitution and transliteration so:
cool... thanks, I am very much a newbie =)
here is the latest:
the end still needs work.
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Fatal;
use File::Find;
finddepth(\&f, ".");
sub f
{
return if /^\./ or /lost\+found/;
my $o = $_ ;
y/A-Z/a-z/;
y/a-z0-9._/_/c;
s/^_+//;
s/\.mpeg$/\.mpg/;
s/\.ram$/\.rm/;
s/\.qt$/\.mov/;
s/\.jpeg$/\.jpg/;
s/_\./\./g;
s/\._/_/g;
y/_//s;
y/.//s;
return if $_ eq $o or -e $_;
if (rename $o, $_)
{
print "\n $File::Find::name -> \n $File::Find::dir/$_\n";
}
else
{
print "failed: $!\n"; die "faild to rename $o to $_, left as $o";
}
}
Re: file renamer... request feedback
am 26.03.2006 00:09:53 von Matt Garrish
"TOC" wrote in message
news:YT6Vf.174576$KG5.54045@fe04.news.easynews.com...
> "John W. Krahn" wrote in news:0r6Vf.5531$K11.1935
> @clgrps12:
>
>> You don't need the for loop because $_ is the default variable for
>> substitution and transliteration so:
>
> cool... thanks, I am very much a newbie =)
> here is the latest:
>
>
> the end still needs work.
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use diagnostics;
> use Fatal;
> use File::Find;
>
> finddepth(\&f, ".");
> sub f
> {
> return if /^\./ or /lost\+found/;
> my $o = $_ ;
> y/A-Z/a-z/;
> y/a-z0-9._/_/c;
> s/^_+//;
> s/\.mpeg$/\.mpg/;
> s/\.ram$/\.rm/;
> s/\.qt$/\.mov/;
> s/\.jpeg$/\.jpg/;
One minor point: you don't need to escape the period on the replacement side
of the expression.
Matt
Re: file renamer... request feedback
am 26.03.2006 19:04:56 von Justin C
On 2006-03-25, TOC wrote:
>
> cool... thanks, I am very much a newbie =)
> here is the latest:
> {
> print "failed: $!\n"; die "faild to rename $o to $_, left as $o";
> }
Do you really want it to die? Don't you want it to go on and rename the
rest of the files? Maybe a 'warn' instead? Or, save the file name to an
array and, after all the successful renaming, output a list of files it
was unable to rename?
Justin.
--
Justin C, by the sea.