file renamer... request feedback

T

TOC

#!/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 $!;
 
U

Uri Guttman

T> my $new = $_;
T> my $old = $_;

i would do that as:

my $old = $_ ;
my $new = $old ;

T> $new = lc $new ;

combine that with the initial assignment:

my $new = lc $old ;

T> $new =~ tr/a-z0-9._/_/c;
T> $new =~ s/^_//g;

why the /g? since it is anchored it can only happen one time

T> $new =~ s/\.mpeg$/mpg/g;

ditto.

and you are losing the . too. do you want that?
T> $new =~ s/\.ram$/rm/g;
T> $new =~ s/\.qt$/mov/g;
T> $new =~ s/\.jpeg$/jpg/g;

more of same.

T> $new =~ s/_\././g;
T> $new =~ s/\._/_/g;

??
some comments on WHY you are doing these transforms would be
useful. this brings up a rule of mine:

code is what, comments are why.

learn and apply that and your code will be much better.

T> $new =~ tr/__/_/s;

that makes little sense. tr takes a character set (similar to [] in s///
but NOT THE EXACT SAME THING) so any dups in there are just redundant.

$new =~ tr/_//s;

this does same thing. the right side will be the same as the left
side. the squeeze option will work on any repeated chars from the right
side.

T> $new =~ tr/.././s;

ditto.

T> return if $new eq $old;
T> # rename($old, $new) or die $!;
T> print "renaming \n $File::Find::name ---> \n $File::Find::dir/$new\n" or
T> die $!;


print rarely fails (full file system is about the only known failure) so
why die on it?

uri
 
R

robic0

T> my $new = $_;
T> my $old = $_;

i would do that as:

my $old = $_ ;
my $new = $old ;

T> $new = lc $new ;

combine that with the initial assignment:

my $new = lc $old ;

T> $new =~ tr/a-z0-9._/_/c;
T> $new =~ s/^_//g;

why the /g? since it is anchored it can only happen one time
Not so cowboy. But to "strip" __ could better be done by s/^_+//
T> $new =~ s/\.mpeg$/mpg/g;

ditto.

and you are losing the . too. do you want that?
T> $new =~ s/\.ram$/rm/g;
T> $new =~ s/\.qt$/mov/g;
T> $new =~ s/\.jpeg$/jpg/g;

more of same.

T> $new =~ s/_\././g;
T> $new =~ s/\._/_/g;

??
some comments on WHY you are doing these transforms would be
useful. this brings up a rule of mine:

code is what, comments are why.

learn and apply that and your code will be much better.

T> $new =~ tr/__/_/s;

that makes little sense. tr takes a character set (similar to [] in s///
but NOT THE EXACT SAME THING) so any dups in there are just redundant.

$new =~ tr/_//s;

this does same thing. the right side will be the same as the left
side. the squeeze option will work on any repeated chars from the right
side.

T> $new =~ tr/.././s;

ditto.

T> return if $new eq $old;
T> # rename($old, $new) or die $!;
T> print "renaming \n $File::Find::name ---> \n $File::Find::dir/$new\n" or
T> die $!;


print rarely fails (full file system is about the only known failure) so
why die on it?

uri
 
U

Uri Guttman

"r" == robic0 <robic0> writes:

r> Not so cowboy. But to "strip" __ could better be done by s/^_+//

there is no spec so you can't change what he did. typical of you to not
get that. also my comment is fine. show me a case where a single
anchored (no alternation) is done more than one time with /g. oh sorry,
you won't know the difference between + and /g so don't bother answering.

just go learn python already. they will welcome with open arms (if they
had arms)

uri
 
T

TOC

Thanks, here is the code with your suggestions Incorporated.




#!/usr/bin/perl
use strict;
use File::Find;
use warnings;
use diagnostics;

finddepth(\&fixnames, ".");

sub fixnames
{
return if /^\./; # do not rename dotfiles
return if /lost\+found/; # do not rename this system
directory
my $old = $_ ; # preserve original name
my $new = lc $old ; # preserve new name, and lowercase
it at the same time
$new =~ tr/a-z0-9._/_/c; # translate all characters to _
except 'a-z 0-9 . _'
$new =~ s/^_+//; # remove all leading _'s
$new =~ s/\.mpeg$/\.mpg/; # fix odd extensions
$new =~ s/\.ram$/\.rm/;
$new =~ s/\.qt$/\.mov/;
$new =~ s/\.jpeg$/\.jpg/;
$new =~ s/_\./\./g; # replace _. with .
$new =~ s/\._/_/g; # replace ._ with _
$new =~ tr/__//s; # squeeze repeated _'s
$new =~ tr/..//s; # squeeze repeated .'s
return if $new eq $old; # abort if filname has not changed
return if (-e $new); # abort if new name already exists
(i would like to change increment the file and try again somehow)
rename($old, $new) or warn "Cannot rename $old:$!\n"; # I wonder
if I should do something like --> rename($was, $_) unless $was eq $old;
print "renaming \n $File::Find::name ---> \n $File::Find::dir/$new\n";
# show the files that we are renaming
 
U

Uri Guttman

T> Thanks, here is the code with your suggestions Incorporated.

T> my $old = $_ ; # preserve original name
T> my $new = lc $old ; # preserve new name, and lowercase
T> it at the same time

preserve? those are assignments, not preservations.

T> $new =~ s/_\./\./g; # replace _. with .
T> $new =~ s/\._/_/g; # replace ._ with _

read those comments carefully. are they WHAT or WHY?

T> $new =~ tr/__//s; # squeeze repeated _'s
T> $new =~ tr/..//s; # squeeze repeated .'s

you didn't read the part about tr/..// being the same as tr/.//

T> print "renaming \n $File::Find::name ---> \n $File::Find::dir/$new\n";
T> # show the files that we are renaming

your comments are mostly what. try to learn how to say why. ask yourself
the question, WHY am i replacing _. with . and put that answer in the
comment.

uri
 
R

robic0

r> Not so cowboy. But to "strip" __ could better be done by s/^_+//

there is no spec so you can't change what he did. typical of you to not
get that. also my comment is fine. show me a case where a single
anchored (no alternation) is done more than one time with /g. oh sorry,
you won't know the difference between + and /g so don't bother answering.

just go learn python already. they will welcome with open arms (if they
had arms)

uri

Uri, $new =~ s/^_//g; re-anchors after the substitution, minus 1 '_'.
You hate me and thats ok man. There isin't anybody I haven't insulted
on this group, and your probably one of them. I'm not saying thats right,
but the knowledge moves faster when the adrenaline flows. I like to cover
all the bases, from trivial to breaking down complexity.
I kick back when I'm kicked...

roboc0
 
J

John Bokma

for ( $new ) {

tr/a-z0-9._/_/c; # translate all characters to
s/^_+//; # remove all leading _'s
s/\.mpeg$/\.mpg/; # fix odd extensions
:
:
tr/..//s; # squeeze repeated .'s
}

$new eq $old and return;
-e $new and return;
 
J

John Bokma

Uri Guttman said:
just go learn python already. they will welcome with open arms (if
they had arms)

uncalled for, don't turn this in a language war.

KF robic0 or ignore it
 
T

TOC

T> my $old = $_ ; # preserve original name
T> my $new = lc $old ; # preserve new name, and
lowercase T> it at the same time

preserve? those are assignments, not preservations.


um.. my terminology.. I guess it is confusing, I'll change it.

T> $new =~ s/_\./\./g; # replace _. with .
T> $new =~ s/\._/_/g; # replace ._ with _

read those comments carefully. are they WHAT or WHY?


well the overall purpose of the script is to make the names pretty...
according to what I define as pretty =)

T> $new =~ tr/__//s; # squeeze repeated _'s
T> $new =~ tr/..//s; # squeeze repeated .'s

you didn't read the part about tr/..// being the same as tr/.//


yes I did =)... after I posted that... lol


T> print "renaming \n $File::Find::name ---> \n
$File::Find::dir/$new\n"; T> # show the files that we are
renaming

your comments are mostly what. try to learn how to say why. ask
yourself the question, WHY am i replacing _. with . and put that
answer in the comment.

uri


well I am replacing ._ with _ and _. with . to fix the following examples:

my_file_.txt
my_other._file.txt


thanks for your help, by the way.
 
R

robic0

for ( $new ) {

tr/a-z0-9._/_/c; # translate all characters to
s/^_+//; # remove all leading _'s
s/\.mpeg$/\.mpg/; # fix odd extensions
:
:
tr/..//s; # squeeze repeated .'s
}

$new eq $old and return;
-e $new and return;

John, I'm out of work, anything you can turn me on to?
 
T

TOC

for ( $new ) {

tr/a-z0-9._/_/c; # translate all characters to
s/^_+//; # remove all leading _'s
s/\.mpeg$/\.mpg/; # fix odd extensions
:
:
tr/..//s; # squeeze repeated .'s
}

$new eq $old and return;
-e $new and return;

thanks.... here is the latest... forgive the lack of comments, I am working
on them...



#!/usr/bin/perl
use strict;
use File::Find;
use warnings;
use diagnostics;

finddepth(\&fixnames, ".");

sub fixnames
{
return if /^\./;
return if /lost\+found/;
my $o = $_ ;
my $n = lc $o ;
for ( $n )
{
tr/a-z0-9._/_/c;
s/^_+//;
s/\.mpeg$/\.mpg/;
s/\.ram$/\.rm/;
s/\.qt$/\.mov/;
s/\.jpeg$/\.jpg/;
s/_\./\./g;
s/\._/_/g;
tr/_//s;
tr/.//s;
}
$n eq $o and return;
-e $n and return;
# rename($o, $n) or warn "Cannot rename $o:$!\n";
print "renaming \n $File::Find::name ---> \n $File::Find::dir/$n\n";
 
T

TOC

more improvements, thanks for all the feedback, keep it coming =)








#!/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";
 
T

Tintin

TOC said:
more improvements, thanks for all the feedback, keep it coming =)
#!/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";

Are you playing golf now?

Why change $old to $o and $new to $n? The small saving in keystrokes
doesn't help make it any more readable.
 
T

TOC

Are you playing golf now?

Why change $old to $o and $new to $n? The small saving in keystrokes
doesn't help make it any more readable.



golf is cool, I wish I was good enough to play... someday.


here is a golfish answer: (spaces -> underscores only)


#!/usr/bin/perl -nl
$o=$_;y/ /_/;-e||rename$o,$_
 
R

robic0

more improvements, thanks for all the feedback, keep it coming =)








#!/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;
I'm drunk after my last ISP rant but
not saying it doesen't exitst but what does y// do?
I'm sure it does somethig, I'm no expert though..
 
T

TOC

robic0 wrote in
I'm drunk after my last ISP rant but
not saying it doesen't exitst but what does y// do?
I'm sure it does somethig, I'm no expert thoug






it is an alias of tr

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";
}
}
 
R

robic0

Are you playing golf now?

Why change $old to $o and $new to $n? The small saving in keystrokes
doesn't help make it any more readable.

I'm missing the &'s you name (except for $o). Is too much cut from
the quote?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top