If you&#39;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&#39;t mean it&#39;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&#39;t like that, keep the steps separate:<br>
<br>my $newname = $oldname;<br>$newname =~ s/...<br><br>If you only want field &#39;10&#39; 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 &#39;%y_%m_%d&#39;, $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&#39;t capturing the xurrent name to reuse in the replace portion. Worst of all, this isn&#39;;t a search-and-replace problem at all<br>
<br>my $newname = $date . &#39;_&#39; . $oldname<br><br><br><br><div class="gmail_quote">On Wed, Sep 16, 2009 at 6:37 PM, Chris Jones <span dir="ltr">&lt;<a href="mailto:cj@cr-jay.ca">cj@cr-jay.ca</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">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>
dsc_0023.jpg becomes 16_09_09_dsc_0023.jpg.<br>
<br>
I am sure the following can be improved on:<br>
#!/usr/bin/perl<br>
<br>
use strict;<br>
use warnings;<br>
use Date::Format;<br>
<br>
my $dir = &#39;.\&#39;;<br>
my $date;<br>
<br>
my @info;<br>
# Time in seconds since epoch<br>
# atime = info[8]<br>
# mtime = info[9]<br>
# ctime = info[10]<br>
<br>
# Convert epoch timestamp to YYYYMMDD<br>
my $created;<br>
<br>
my @fileList = glob &quot;${dir}???_????.jpg*&quot;;<br>
<br>
foreach (@fileList) {<br>
    next if -d;<br>
    @info = stat($_) || die &quot;Error $!&quot;;<br>
    $date = time2str(&#39;%Y%m%d&#39;, $info[10]);<br>
    my $oldname = $_;<br>
    s/&quot;???_???.jpg&quot;/&quot;$date_???_???.jpg&quot;/;     ##### &lt;===<br>
    rename $oldname, $_  or<br>
        $_ = $oldname,<br>
        warn $_, &#39; not renamed: &#39;, $!;<br>
}<br>
<br>
<br>
<br>
Chris Jones<br>
14 Oneida Avenue<br>
Toronto, ON M5J 2E3.<br>
Tel.  416-203-7465<br>
Fax. 416-946-1005<br>
<br>
<br>
_______________________________________________<br>
toronto-pm mailing list<br>
<a href="mailto:toronto-pm@pm.org" target="_blank">toronto-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/toronto-pm" target="_blank">http://mail.pm.org/mailman/listinfo/toronto-pm</a><br>
</blockquote></div><br>