Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: processing dates as directories

by farang (Chaplain)
on Aug 10, 2013 at 02:15 UTC ( #1048865=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1048865]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2023-02-05 22:51 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (33 votes). Check out past polls.