Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Optimize file renaming.

by ivancho (Hermit)
on May 26, 2005 at 12:02 UTC ( [id://460631]=note: print w/replies, xml ) Need Help??


in reply to Optimize file renaming.

chdir("e:\\perl\\comics\\"); foreach my $fname (glob("*.png")) { my $append = "0" x (7 - length($fname)); rename($fname, $append.$fname) or die "Cannot rename $fname"; }
Update:

in all honesty, for my usage I'd have written

perl -e 'rename($_,("0"x(7-length($_))).$_) for glob("*.png")'
from the appropriate directory

But it's probably best to maintain some readability when messing around with the filesystem

Update2:

rename($_,substr("000$_",-7)) is even groovier ( borrowed from EdwardG ), although it'll mess you up, if there are longer names, while the previous version won't

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://460631]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2025-05-23 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.