file renamer... request feedback

R

robic0

robic0 wrote in






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";
}
}
Your smarter than me TOC...
robic0
 
T

TOC

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



are you talking about this?

#!/usr/bin/perl -nl
$o=$_;y/ /_/;-e||print("renaming $o => $_")&&rename$o,$_

If not, I am not sure what you are asking.
 
R

robic0

robic0 wrote in



are you talking about this?

#!/usr/bin/perl -nl
$o=$_;y/ /_/;-e||print("renaming $o => $_")&&rename$o,$_

If not, I am not sure what you are asking.
Over my head but I'm lazy, don't want to read unless I have to.
It's my eyeballs, they aren't round, not quite square either..
I think its my brains protection. All it wants to do is think,
sheesh...
 
R

robic0

Over my head but I'm lazy, don't want to read unless I have to.
It's my eyeballs, they aren't round, not quite square either..
I think its my brains protection. All it wants to do is think,
sheesh...

I just wan't to follow up here a little.
I am blasted so I may read this tommorow and decided it was a stupid
thing to write.
Although this line:
$o=$_;y/ /_/;-e||print("renaming $o => $_")&&rename$o,$_ (complete?)
might look intimidating to me at first glance, especially the "&&",
I would not like to have to figure this out, especially with
suspect imbed Unix switches >>#!/usr/bin/perl -nl
when I'm a windows guy you know?

I'm so lazy, I let my 170 IQ sleep in 2 hours while I go
for a 4 mile jog..

robic0
 
T

TOC

#!/usr/bin/perl -nl
I just wan't to follow up here a little.
I am blasted so I may read this tommorow and decided it was a stupid
thing to write.
Although this line:
$o=$_;y/ /_/;-e||print("renaming $o => $_")&&rename$o,$_ (complete?)
might look intimidating to me at first glance, especially the "&&",
I would not like to have to figure this out, especially with
suspect imbed Unix switches >>#!/usr/bin/perl -nl
when I'm a windows guy you know?

I'm so lazy, I let my 170 IQ sleep in 2 hours while I go
for a 4 mile jog..

robic0



Deparse is your friend:

# perl -MO=Deparse f.pl

BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
$o = $_;
tr/ /_/;
print "renaming $o => $_" and rename $o, $_ unless -e $_;
}
f.pl syntax OK
 
R

robic0

Deparse is your friend:

# perl -MO=Deparse f.pl

BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
$o = $_;
tr/ /_/;
print "renaming $o => $_" and rename $o, $_ unless -e $_;
}
f.pl syntax OK

so rename(uh?) a file (with underscores, formerly spaces) unless it exists?
cool..
 
D

Dr.Ruud

John Bokma schreef:
Uri Guttman:

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

I read it as a {{{{joke}}}}. Why should Perl get all of 0?

--
Affijn, Ruud (has a wife called Joke [yoka], which in her case
could be short for Johanna Cornelia).

"Gewoon is een tijger."
echo 014C8A26C5DB87DBE85A93DBF |perl -pe 'tr/0-9A-F/JunkshoP cartel,/'
 
D

Dr.Ruud

TOC schreef:
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"; }

if ($_ eq $o) {...}
elsif (rename $o, $_) {...}
else {...}

And maybe, with some clever tricks, you could write it as

if ($o.eq) {...}
elsif ($o.mv) {...}
else {...}

And I still forgot to test $_ for pre-existance.
 
T

Tad McClellan

[ missing a couple levels of attribution, please don't do that ]

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


Your response leads me to believe that you have missed the point
that Uri was trying to make.

The code says to replace _. with . and the comment says to
replace _. with .

Why bother to say the same thing twice?

The comments are where you get to specify what you define to be "pretty".


eg:

$new =~ s/_\./\./g; # underscores next to dots are ugly (not pretty)
$new =~ s/\._/_/g;

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


"to fix" kinda requires an explanation of what is broken
in the first place...

my_file_.txt
my_other._file.txt


eg:

well I am replacing ._ with _ and _. with . because
underscores next to dots are ugly


(and your code won't "fix" my_file__.txt )
 
A

Anno Siegel

Dr.Ruud said:
TOC schreef:


if ($_ eq $o) {...}
elsif (rename $o, $_) {...}
else {...}

And maybe, with some clever tricks, you could write it as

if ($o.eq) {...}
elsif ($o.mv) {...}
else {...}

Huh? What are those dots in the conditionals? Perl 6 method calls?
And I still forgot to test $_ for pre-existance.

Another mystery remark, given the non-context :)

Anno
 
D

Dr.Ruud

Anno Siegel schreef:
Dr.Ruud:

Huh? What are those dots in the conditionals? Perl 6 method calls?

Close, they are from the next neighbourhood, where they worship $_ but
can't refer to Her by Her name.

Another mystery remark, given the non-context :)

if ($_ ne $o)
{
if (-e $_) {...} # pre-existance
elsif (rename $o, $_) {...} # try rename
else {...} # failed
}
 
T

TOC

Anno Siegel schreef:

Close, they are from the next neighbourhood, where they worship $_ but
can't refer to Her by Her name.



if ($_ ne $o)
{
if (-e $_) {...} # pre-existance
elsif (rename $o, $_) {...} # try rename
else {...} # failed
}



ok, first of all I don't understand the ... can you give me a link to
where it is documented?


here is the current state of the script, is this what you had in mind?


!/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;
if ($_ ne $o)
{
if (-e $_) {print "\n $File::Find::dir/$_ already exists \n";}
elsif (rename $o, $_) {print "\n $File::Find::name -> \n
$File::Find::dir/$_\n";}
else {print "\n failed to rename $o to $_, left as $o\n ";}
}
}
 
D

Dr.Ruud

TOC schreef:
Dr.Ruud:

ok, first of all I don't understand the ... can you give me a link to
where it is documented?

Those are meant as ellipses, to be replaced by any code you fancy.

here is the current state of the script, is this what you had in mind?

Yes. Maybe you also want to print that you skipped some file, if ($_ eq
$o).
 
L

Lukas Mai

robic0 schrob:
Uri, $new =~ s/^_//g; re-anchors after the substitution, minus 1 '_'.

No, it doesn't. Uri is right. You are completely wrong and I have no
idea why you think /g changes how anchors work.
You hate me and thats ok man. There isin't anybody I haven't insulted

I don't hate you, I hate obviously incorrect postings ("obviously"
because the docs say something else and a one-line test script would
show the docs to be right).
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.

Content free.
I kick back when I'm kicked...

Less kicking, more reading plz. Remember, I pwn your soul.

Thanks, Lukas
 
R

robic0

robic0 schrob:

No, it doesn't. Uri is right. You are completely wrong and I have no
idea why you think /g changes how anchors work.
So what your saying is that after the first substitution in global context
s///g, the char position pointer has no notion of beggining of line?

Well, I think it could be written that /^/ is still valid after every
substitution.

Or are you saying the documentation says /^/ is *ONLY* valid before the
*FIRST* substitution in global context?

robic0
 
R

robic0

Huh? What are those dots in the conditionals? Perl 6 method calls?
I'm still on the 5.8 strain.
Oh God no, please no 'C' style class method calls in 6.
Say it ain't so...
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top