jcb beat me to the point about collisions from different users. (Edit: it's late here and I got things reversed in my head. No need for the reverse for the sort.) Here is a simple solution that works except it will clobber any collisions between users with the user with the lowest highest number. This solution takes a reverse sort of the filenames and then uses File::Copy::move to rename them so that the newest collision file is the last one to be renamed. If you specify what to do by user collisions this could be modified to watch for the user number.

Perl has a built in rename function but the documentation suggests using File::Copy::move since it is more portable across operating systems.

use warnings; use strict; use File::Copy; use File::Glob ':glob'; mkdir 'test' unless -d 'test'; #foreach( reverse sort glob( "*_Table_*.pdf" ) ){ foreach( sort glob( "*_Table_*.pdf" ) ){ print "$_\n"; my $newname = $_; $newname =~ s/^[0-9_]+//; print "--$newname\n"; ## using copy for testing. copy $_, "./test/$newname" or print "Error copying <$_> $!\n"; #move $_, "$newname" or print "Error renaming <$_> $!\n"; } __DATA__ test files: 8_2007_5_22_15_34_23_Table_-_2007522_XYZ_W3.pdf 8_2007_5_22_22_34_12_Table_-_2007522_XYZ_W3.pdf 7_2007_5_22_16_35_23_Table_-_2007522_XYZ_W3.pdf 7_2007_5_22_23_36_12_Table_-_2007522_XYZ_W3.pdf output file: Table_-_2007522_XYZ_W3.pdf

Edit: I forgot to add the program output. Also, I had an extra file in my test files.

8_2007_5_22_22_34_12_Table_-_2007522_XYZ_W3.pdf --Table_-_2007522_XYZ_W3.pdf 8_2007_5_22_15_34_23_Table_-_2007522_XYZ_W3.pdf --Table_-_2007522_XYZ_W3.pdf 7_2007_5_22_22_34_12_Table_-_2007522_XYZ_W3.pdf --Table_-_2007522_XYZ_W3.pdf 7_2007_5_22_15_34_23_Table_-_2007522_XYZ_W3.pdf --Table_-_2007522_XYZ_W3.pdf 7_2007_12_22_15_34_23_Table_-_20071222_XYZ_W3.pdf --Table_-_20071222_XYZ_W3.pdf

Edit: updated output without reverse sort.

7_2007_12_22_15_34_23_Table_-_20071222_XYZ_W3.pdf --Table_-_20071222_XYZ_W3.pdf 7_2007_5_22_15_34_23_Table_-_2007522_XYZ_W3.pdf --Table_-_2007522_XYZ_W3.pdf 7_2007_5_22_22_34_12_Table_-_2007522_XYZ_W3.pdf --Table_-_2007522_XYZ_W3.pdf 8_2007_5_22_15_34_23_Table_-_2007522_XYZ_W3.pdf --Table_-_2007522_XYZ_W3.pdf 8_2007_5_22_22_34_12_Table_-_2007522_XYZ_W3.pdf --Table_-_2007522_XYZ_W3.pdf

In reply to Re: Batch file renaming - on identical name, keep only most recent file, based on dates by Lotus1
in thread Batch file renaming - on identical name, keep only most recent file, based on dates by Anonymous Monk

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.