http://qs1969.pair.com?node_id=1048865


in reply to processing dates as directories

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; ...