I was playing around with this earlier so I may as well post what I did. It's a potential beginning, with a minimal amount of error checking. It takes just one argument for a start_date which must be eight digits in length, and operates on all files starting with '20' plus six more digits where the date/filename is at or larger than the start_date. It doesn't do a sanity check on how many files will be operated on, though. It can be tested as is to see a list of which files would be affected depending on the command line argument given.

use strict; use warnings; my $start_date = $ARGV[0] || 'bogus'; die "bad argument" unless $start_date =~ /\d{8}/; my $archive_dir = '/dir/archive/'; my @files = qx('ls' $archive_dir); chomp @files; @files = sort {$b <=> $a} grep /^20\d{6}$/, @files; for my $file( @files ){ last if $file < $start_date; my $fullpath = $archive_dir . $file; print "$fullpath\n"; # for testing # Issue shell cmds here, e.g. # qx( 'cp' $fullpath $fullpath'.bkup'); # to back up files before proceeding. }

The sort line puts the @files array in descending numerical order, so when the for loop runs it'll pick them up from newest to oldest. Obviously you'd want to test whatever shell processing is being done before letting it do anything potentially destructive to existing data, so making backup copies is probably prudent.

Update: The easiest way I can think of right now, if a little repetitious, to include an end_date would be to start the program with the following.

my $start_date = $ARGV[0] || 'bogus'; die "bad argument" unless $start_date =~ /\d{8}/; my $end_date = $ARGV[1] || 'bogus'; die "bad argument" unless $end_date =~ /\d{8}/; ...
And then add a next condition in the for loop to skip processing for files beyond the end_date.
next if $file > $end_date; last if $file < $start_date; ...


In reply to Re: processing dates as directories by farang
in thread processing dates as directories by moesplace

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.