I found that whenever I'm not able to clearly lay out a problem I'm trying to tackle in writing, this is a sure sign of a serious misconception of the whole issue on my part. I command your effort to explain your specific problem an extra time. ;-)

So, paraphrasing your problem specification, I get this:

1. Rotate files from dir_1 to dir_2 every 5 minutes.
2. Rotate files from dir_2 to dir_3 every 5 minutes.
3. Rotate files from dir_3 to dir/main every 5 minutes.
4. and so on.

A way to solve the problem might be to write a simple Perl daemon to do just that. Let it sleep for 5 minutes after which it'll go about rotating the files. The way it would track when a file had been last moved (and thus when it's next move is to happen) is by examining it's creation time stamp (as this changes whenever a file is moved/created, or am I wrong?).

Let me think some more.. and hopefully I can get a script to you (unless another monk beats me to it ;-)

Here's basic algorithm, though:

1. Study files in all directories.
2. Order files by their creation time.
3. Calculate next earliest time when a bunch of files has to be rotated.
4. Sleep...
5. (wake up), rotate files.. and go to step 1.


UPDATE: Here's a very dirty attempt at the code you might be looking at:
use strict; use File::Find; # plug your own value here $rotate_dir = '/share/files/rotate'; $move_timeout = 5*60; # 5 minutes. my %files; while (1) { # find files that have to be rotated my @rotate; find(\&wanted, $rotate_dir); rotate(); } sub wanted { if (exists $files{$File::File::name}) { # alright, we saw it before.. so, has it's # time come to move? push @rotate, $File::File::name if ($files{$File::File::name} < time - $move_timeout); } $files{$File::File::name} = time; } sub rotate { # "I know, I don't have to use global vars, I KNOW!" ;) for (@rotate) { my $new_path; if (m|/main/|) { # initial spot $new_path = $_; # drop it to the first sale rep's directory $new_path =~ s|/main/|/dir_1/|; } else { # retrieve sales person's id my ($sales_id) = ($_ =~ m|(\d+)/$|); my $new_id = $sales_id + 1; $new_dir =~ s|$sales_id/|$new_id/|; # drop to main directory if next sale's rep # id isn't valid (exists) $new_dir = 'dir/main' unless (-d $new_dir); } # move to that new directory... }
Things left to do (other than actually 'finish' the code) is to also make sure your daemon won't chew up all your system resources while doing it's work. For that, I'd suggest making it a 'nice' process using the set_priority() method with a higher value. Also, please be forewarned that I didn't even have the chance to compile the code (actually, I'm in a rush now to do other work.. err.. 'at my real work' ;)

NOTE: I also assumed that 5 minute 'time limit' applies to each file individually. Therefore, inside my code I gather statistics on each file.

_____________________
$"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}

In reply to Re: sequential file handling (again) by vladb
in thread sequential file handling (again) by airblaine

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.