Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: file renamer (i'm a newbie)

3 views
Skip to first unread message
Message has been deleted

Andrew Savige

unread,
Mar 24, 2006, 7:52:53 PM3/24/06
to Eric Waguespack, go...@perl.org
--- Eric Waguespack wrote:
> Currently the file below renames files by changing spaces to underscores.
> usage:
> find | ./file_name_fixer.pl
>
> how could it be shortened up?
>
> I would eventually like to modify it so that it will will modify names
> so that they only contain 'a-z0-9._'

For some general solutions to renaming files, see Larry's historic
rename script:

http://www.csit.fsu.edu/~burkardt/pl_src/rename/rename.html

Aristotle's modern facelift of that script at:

http://www.perlmonks.org/?node_id=303814

and the Unix rename faq:

http://www.faqs.org/faqs/unix-faq/faq/part2/section-6.html

Renaming files is also discussed in Perl Cookbook, recipe 9.9.

I think the begi...@perl.org mailing list is more appropriate
for this type of question.

HTH,
/-\



____________________________________________________
On Yahoo!7
Messenger - Make free PC-to-PC calls to your friends overseas.
http://au.messenger.yahoo.com

Andrew Savige

unread,
Mar 25, 2006, 12:42:35 AM3/25/06
to Eric Waguespack, go...@perl.org
--- Eric Waguespack wrote:
> I was asking how to shorten it up to a one-liner using various perl
> idioms...

OK, I was just trying to protect you from the madness that is Perl
golf. ;-) If you don't believe me, you can read about some of the
history of Perl golf here:

http://www.perlmonks.org/?node_id=437032

To help you learn about Perl one-liners, there are plenty of links
to choose from here:

http://www.perlmonks.org/?node_id=502295

And, of course, the most unreadable collection of Perl one liners
ever created:

http://terje2.perlgolf.org/~golf-info/Book.html

(which seems to be down -- or has it moved?).

and cog's secret operator thread:

http://www.mail-archive.com/f...@perl.org/msg03431.html

Oh, and apart from that, don't forget to read:

perldoc perlrun

Now to shortening your rename program.

First, notice that there is a bug in your version in that
s/ /_/ only changes the first space (and so will not work
correctly with filenames containing multiple spaces).

Some straightforward shortening without changing the algorithm:

#!/usr/bin/perl -nl
-e or print("$_ <=== does not exist"),next;
$o=$_;
tr/ /_/ or next;
-e and print("$_ <=== already exists"),next;
print"renaming $o ===> $_";
rename$o,$_

As a one-liner, this may be adequate:

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

Message has been deleted
Message has been deleted

Andrew Savige

unread,
Mar 25, 2006, 2:44:20 AM3/25/06
to Eric Waguespack, go...@perl.org
--- Eric Waguespack wrote:
> ok...

>
> #!/usr/bin/perl -nl
> $o=$_;y/ /_/;-e||print("renaming $o ===> $_")&&rename$o,$_
>
>
> ok so you are looping around <> and adding newlines...
> saving the name as $o
> y is an alias for tr
> checking if the file exists with -e
> || print msg && rename.
>
> very nice, thanks a lot.

Yep. To aid sight in this case, 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

Cheers,

Message has been deleted

Philippe 'BooK' Bruhat

unread,
Mar 30, 2006, 4:25:10 AM3/30/06
to Eric Waguespack, go...@perl.org
Le samedi 25 mars 2006 à 00:07, Eric Waguespack écrivait:
> You are really helpful... thanks for all the info.
>
> here is my latest code... now l'll take a few minutes to digest your
> suggestions.
>
> # fix odd extensions
> $new =~ s/\.mpeg$/\.mpg/;
> $new =~ s/\.ram$/\.rm/;
> $new =~ s/\.qt$/\.mov/;
> $new =~ s/\.jpeg$/\.jpg/;

To handle lots of extensions without having to edit your code, I'd suggest
using a dispatch table.

# initialise the table
my %ext = (
mpeg => 'mpg',
ram => 'rm',
qt => 'mov',
jpeg => 'jpg',
);

# build up a single regexp
my $ext_re = qr/\.(@{[join'|', sort keys %ext]})$/;

and in your function:

# fix odd extensions
$new =~ s/$ext_re/$ext{$1}/;

When your list of extensions grows, you only have to update the table.
And you can imagine a script that reads the table content from a
configuration file, which gives you even more flexibility.

By the way, golfers, do we have a name for the secret circumfix
@{[]} operator?

--
Philippe "BooK" Bruhat

Even the most powerful being is at the mercy of the weakest.
(Moral from Groo The Wanderer #34 (Epic))

Jasper

unread,
Mar 30, 2006, 5:56:12 AM3/30/06
to Philippe 'BooK' Bruhat, Eric Waguespack, go...@perl.org
On 3/30/06, Philippe 'BooK' Bruhat <philipp...@free.fr> wrote:

> By the way, golfers, do we have a name for the secret circumfix
> @{[]} operator?

A derefarrance? (deref-array-nce) ?

> Even the most powerful being is at the mercy of the weakest.
> (Moral from Groo The Wanderer #34 (Epic))

From Groo? Surely this should read "most clever being is at the mercy
of the stupidest"?

--
Jasper

0 new messages