Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-01-25 07:06 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (65 votes). Check out past polls.