[tpm] Renaming files

Tom Legrady legrady at gmail.com
Wed Sep 16 16:16:55 PDT 2009


If you're going to assign $_ to $oldname, why not do it straight off the
bat? I believe in always naming variables, anyway. Just because some
commands will fill in $_ automatically doesn't mean it's the best way to do
things.

for my $oldname ( @fileList ) { ....

Then you can copy the old name to the new name and re-work it in one step.

my ( $newname = $oldname ) =~ s/.....

or if you really don't like that, keep the steps separate:

my $newname = $oldname;
$newname =~ s/...

If you only want field '10' of stats, why capture the whole array?

my $info = ( stats $oldname )[10]

Your date formatting does not provide the underscores your specification
says you want:

$date = time2str '%y_%m_%d', $info[10];



Most importantly, the search-and-replace is wrong. There are no quotes in
the name. There are four digits not three. You aren't capturing the xurrent
name to reuse in the replace portion. Worst of all, this isn';t a
search-and-replace problem at all

my $newname = $date . '_' . $oldname



On Wed, Sep 16, 2009 at 6:37 PM, Chris Jones <cj at cr-jay.ca> wrote:

> I want to go to the DOS prompt and rename all the files in that directory
> with a date prefix if there is no prefix there:
>
> dsc_0023.jpg becomes 16_09_09_dsc_0023.jpg.
>
> I am sure the following can be improved on:
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use Date::Format;
>
> my $dir = '.\';
> my $date;
>
> my @info;
> # Time in seconds since epoch
> # atime = info[8]
> # mtime = info[9]
> # ctime = info[10]
>
> # Convert epoch timestamp to YYYYMMDD
> my $created;
>
> my @fileList = glob "${dir}???_????.jpg*";
>
> foreach (@fileList) {
>    next if -d;
>    @info = stat($_) || die "Error $!";
>    $date = time2str('%Y%m%d', $info[10]);
>    my $oldname = $_;
>    s/"???_???.jpg"/"$date_???_???.jpg"/;     ##### <===
>    rename $oldname, $_  or
>        $_ = $oldname,
>        warn $_, ' not renamed: ', $!;
> }
>
>
>
> Chris Jones
> 14 Oneida Avenue
> Toronto, ON M5J 2E3.
> Tel.  416-203-7465
> Fax. 416-946-1005
>
>
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20090916/994fffd9/attachment.html>


More information about the toronto-pm mailing list