in reply to Optimize file renaming.
Update: Analyzing more precisely your script...
It's better, for various reasons to long to be discussed here, to#!/usr/bin/perl -w use strict;
instead of -w. With recent enough perls, that is...use warnings;
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.opendir(THISDIR,"e:\\perl\\comics\\"); my @comics=readdir(THISDIR); close(THISDIR);
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
Huh?!? Why reverse?!?foreach (reverse(@comics)) {
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.my ($name,$ext)=split(/\./,$_);
<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, $_'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Optimize file renaming.
by omega_monk (Scribe) on May 26, 2005 at 13:32 UTC | |
by blazar (Canon) on May 26, 2005 at 13:48 UTC |