[tpm] Renaming files

Chris Jones cj at cr-jay.ca
Wed Sep 16 15:37:15 PDT 2009


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




More information about the toronto-pm mailing list