Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: processing dates as directories

by farang (Hermit)
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; ...


Comment on Re: processing dates as directories
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (15)
As of 2014-12-22 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (121 votes), past polls