<html>
<body>
Thank you. That was exactly the kind of optimization I was looking
for.<br><br>
<br>
At 07:16 PM 9/16/2009, you wrote:<br>
<blockquote type=cite class=cite cite="">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.<br><br>
for my $oldname ( @fileList ) { ....<br><br>
Then you can copy the old name to the new name and re-work it in one
step.<br><br>
my ( $newname = $oldname ) =~ s/.....<br><br>
or if you really don't like that, keep the steps separate:<br><br>
my $newname = $oldname;<br>
$newname =~ s/...<br><br>
If you only want field '10' of stats, why capture the whole
array?<br><br>
my $info = ( stats $oldname )[10]<br><br>
Your date formatting does not provide the underscores your specification
says you want:<br><br>
$date = time2str '%y_%m_%d', $info[10];<br><br>
<br><br>
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<br><br>
my $newname = $date . '_' . $oldname<br><br>
<br><br>
On Wed, Sep 16, 2009 at 6:37 PM, Chris Jones
<<a href="mailto:cj@cr-jay.ca">cj@cr-jay.ca</a>> wrote:<br>
<dl>
<dd>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:<br><br>
<dd>dsc_0023.jpg becomes 16_09_09_dsc_0023.jpg.<br><br>
<dd>I am sure the following can be improved on:<br>
<dd>#!/usr/bin/perl<br><br>
<dd>use strict;<br>
<dd>use warnings;<br>
<dd>use Date::Format;<br><br>
<dd>my $dir = '.\';<br>
<dd>my $date;<br><br>
<dd>my @info;<br>
<dd># Time in seconds since epoch<br>
<dd># atime = info[8]<br>
<dd># mtime = info[9]<br>
<dd># ctime = info[10]<br><br>
<dd># Convert epoch timestamp to YYYYMMDD<br>
<dd>my $created;<br><br>
<dd>my @fileList = glob "${dir}???_????.jpg*";<br><br>
<dd>foreach (@fileList) {<br>
<dd> next if -d;<br>
<dd> @info = stat($_) || die "Error $!";<br>
<dd> $date = time2str('%Y%m%d', $info[10]);<br>
<dd> my $oldname = $_;<br>
<dd>
s/"???_???.jpg"/"$date_???_???.jpg"/;
##### <===<br>
<dd> rename $oldname, $_ or<br>
<dd> $_ = $oldname,<br>
<dd> warn $_, ' not renamed: ',
$!;<br>
<dd>}<br><br>
<br><br>
<dd>Chris Jones<br>
<dd>14 Oneida Avenue<br>
<dd>Toronto, ON M5J 2E3.<br>
<dd>Tel. 416-203-7465<br>
<dd>Fax. 416-946-1005<br><br>
<br>
<dd>_______________________________________________<br>
<dd>toronto-pm mailing list<br>
<dd><a href="mailto:toronto-pm@pm.org">toronto-pm@pm.org</a><br>
<dd>
<a href="http://mail.pm.org/mailman/listinfo/toronto-pm" eudora="autourl">
http://mail.pm.org/mailman/listinfo/toronto-pm</a><br><br>
</dl></blockquote>
<x-sigsep><p></x-sigsep>
<br>
Chris Jones<br>
14 Oneida Avenue<br>
Toronto, ON M5J 2E3.<br>
Tel. 416-203-7465<br>
Fax. 416-946-1005<br><br>
</body>
</html>