in reply to processing dates as directories

for dir in `ls -dr /dir/archive/201*` do where=`pwd` cd "$dir" echo "your commands" cd "$where" done

Not perl, but a solution, isn't it?

Best regards
McA

Replies are listed 'Best First'.
Re: processing dates as directories
by moesplace (Novice) on Aug 09, 2013 at 21:26 UTC

    Yes, that would be a solution... except for the massive amount of directories. The user that asked for a solution wanted to pass in a start and stop point to the script and I'm having a difficult time wrapping my head around a way to start at a specific location(date), stop at a specific location(date) and process through the directories backwards. Thoughts?

      Hey, your solution gave me a thought. Passing in the date is just a number... could I do a numerical < end-date and > start-date comparison inside the loop that you have?

      How would that look exactly?

        Of course you can. Those directory names only look like dates, but are just strings and you can do sort them and use the ... operator on them.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics

      Good morning moesplace,

      as I have to admit that I just overread a part of your requirement I got the feeling that I'm in dept showing a building block for another way of solving your issue. Some other Monks gave different advices and hints. Mine here is a little different as it uses DateTime and DateTime::Duration to explicitly loop over every day in between your start and end date. So, probably another way to look at your problem. And definitely a Perlish solution showing regexes, using top rated modules, operator overloading:

      #!/usr/bin/perl use strict; use warnings; use DateTime; use 5.010; die "ERROR: You have to provide start- and end-date in format 'YYYYMMD +D'" if @ARGV != 2; my $start_date = $ARGV[0]; my $end_date = $ARGV[1]; my $start_year = 1900; my $start_month = 01; my $start_day = 01; my $end_year = 2100; my $end_month = 01; my $end_day = 01; if($start_date =~ m/^([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])$/) + { $start_year = $1; $start_month = $2; $start_day = $3; } else { die "ERROR: Start Date doesn't match YYYYMMDD"; } if($end_date =~ m/^([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])$/) { $end_year = $1; $end_month = $2; $end_day = $3; } else { die "ERROR: End Date doesn't match YYYYMMDD"; } my $start = DateTime->new( year => $start_year, month => $start_month, day => $start_day, ); my $end = DateTime->new( year => $end_year, month => $end_month, day => $end_day, ); if($end < $start) { die "ERROR: end date is less than start date."; } my $one_day = DateTime::Duration->new(days => 1); for(my $i = $end; $i >= $start; $i -= $one_day) { my $output = sprintf('%04d%02d%02d', $i->year, $i->month, $i->day, ); say $output; # Check if path exists # chdir to it # do what you want # chdir back }

      Best regards
      McA

      UPDATE: The driver in this solution are the generated dates and not the files found which than get compared to the start- and end-date.