Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I didn't notice that detail. Those dates in the example code seem to be something that was just typed in by the OP whereas the filenames seemed to be pasted in so I paid more attention to the filenames. My comment about HH_mm_ss was referring to the actual filenames. If the OP were to provide an example that shows a format such as ?H_?m_?s then I would add a quick fix-it function to normalize those into a sane format and then sort. I still think my approach is a good one, especially for new Perl programmers. Some of my work needs to be supported by folks with limited programming experience or by people with limited Perl knowledge.

IMHO that's also a very dangerous assumption to make.
I'm just doing this for fun and it's the OP's responsibility to check his work. The OP didn't provide much to go by. In my own work I do a lot of validation with logging so that files that don't meet my expectations are skipped. I start with a solution that works for a small data set and then run it on the full set of files and inspect the log.

Update: Here is a version that pads the time stamps with zeros so they sort correctly.

use warnings; use strict; #use File::Copy; foreach( sort map { chomp;s/_(\d)_/_0$1_/g;s/_(\d)_/_0$1_/g;$_ } grep +{/^\d+_\d\d\d\d_\d+_\d+_\d+_\d+_\d+_Table/} <DATA> ){ my $newname = $_; $newname =~ s/^[0-9_]+//; printf "%38s%38s\n",$_,$newname; ## rename here. #move $_, $newname or print "Error renaming <$_> $!\n"; } __DATA__ Test file names 8_2007_5_22_15_34_23_Table_-_2007522_XYZ_W3.pdf 8_2007_5_22_22_34_12_Table_-_2007522_XYZ_W3.pdf 8_2007_5_2_15_34_23_Table_-_200752_XYZ_W3.pdf 8_2007_5_2_22_34_12_Table_-_200752_XYZ_W3.pdf 8_2007_5_2_5_34_23_Table_-_200752_XYZ_W3.pdf 8_2007_5_2_2_34_12_Table_-_200752_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 blanks and other things 0 8_3007 &#8993;&#9675;{}&#9562;

The output looks like this:

7_2007_05_22_16_35_23_Table_-_2007522_XYZ_W3.pdf Table_-_20 +07522_XYZ_W3.pdf 7_2007_05_22_23_36_12_Table_-_2007522_XYZ_W3.pdf Table_-_20 +07522_XYZ_W3.pdf 8_2007_05_02_02_34_12_Table_-_200752_XYZ_W3.pdf Table_-_20 +0752_XYZ_W3.pdf 8_2007_05_02_05_34_23_Table_-_200752_XYZ_W3.pdf Table_-_20 +0752_XYZ_W3.pdf 8_2007_05_02_15_34_23_Table_-_200752_XYZ_W3.pdf Table_-_20 +0752_XYZ_W3.pdf 8_2007_05_02_22_34_12_Table_-_200752_XYZ_W3.pdf Table_-_20 +0752_XYZ_W3.pdf 8_2007_05_22_15_34_23_Table_-_2007522_XYZ_W3.pdf Table_-_20 +07522_XYZ_W3.pdf 8_2007_05_22_22_34_12_Table_-_2007522_XYZ_W3.pdf Table_-_20 +07522_XYZ_W3.pdf

In reply to Re^5: 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-12-09 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?