In addition to the problems noted by others, it seems like sorting first and renaming afterwards would be a waste of effort; "%Y%m%d" will automatically sort correctly in most situations, which is part of the point of doing it that way. Also, if you start renaming your files and run into a duplicate date part-way through, you'll end up with a scattered list of filenames. It would be better to validate before you ever start renaming. You might want to consider something like this:

#!/usr/bin/perl -w use strict; use POSIX; my %seen; while (<*.gz>){ my $date = strftime "%Y%m%d", localtime((lstat($_))[9]); my $was = $_; s/\d+/$date/; die "File '$_' already exists!\n" if -f $_; die "We already have a file with that date ($date).\n" if $seen{$_ +}; $seen{$_} = $was; } for my $fn (sort keys %seen){ rename $seen{$fn}, $fn; print "$fn\n"; }

--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf

In reply to Re: rename files with mtime by oko1
in thread rename files with mtime by pbaumgar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.