Hint: sprintf

Update: Analyzing more precisely your script...

#!/usr/bin/perl -w use strict;
It's better, for various reasons to long to be discussed here, to
use warnings;
instead of -w. With recent enough perls, that is...
opendir(THISDIR,"e:\\perl\\comics\\"); my @comics=readdir(THISDIR); close(THISDIR);
Well, nothing wrong with this, per se. But I have the impression that people tend to abuse opendir & C. where a simple glob would do. In particular since you say that your script did work, you must have run it from e:\perl\comics. Otherwise you should have chdir'd there or prepended the dirname to the filenames subsequently.

Also, and this is IMHO important, perl supports "/" as the directory separator also under Windows, which is useful. Quoting backaslashes can be confusing and may lead to errors. If you really want to use them, and of course if you don't need interpolation, you can use single quotes, which hopefully will make your life easier.

All in all you may have had e.g.

my @comics=glob 'e:/perl/comics/*'; # or 'e:/perl/comics/*.png', according to your description
foreach (reverse(@comics)) {
Huh?!? Why reverse?!?
my ($name,$ext)=split(/\./,$_);
Nothing wrong with this either, but a very useful module for "this kinda things" is File::Basename. Now I use it even when a simple regex would do. In any case it makes me sure it's doing the right thing.

<snip rest of code>

Don't! Use sprintf as hinted above, instead.

All in all, if this was just a quick hack, I would have done that with a combination of shell cmds and perl, e.g.:

ls -1 *.png | perl -lne '$o=$_; s/(\d+)/sprintf "%03d", $1/e; rename $ +o, $_'

In reply to Re: Optimize file renaming. by blazar
in thread Optimize file renaming. by omega_monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.