http://www.perlmonks.org?node_id=1061456

mytutorial has asked for the wisdom of the Perl Monks concerning the following question:

I need to retrieve files from a directory on a remote server that are at least 5 minutes old. I am using Net::SFTP::Foreign in my Perl script. The array @directory_info contains the directory path and the filemask. I am pulling the files from the remote server using $sftp->glob. How can I only get the files that are at least 5 minutes old using $sftp->glob? The $sftp->glob portion from my script is below.

while($i < 11) { $directory = "$directory_info[$i][0]"; $filemask = "$directory_info[$i][1]"; $sftp->setcwd($directory) or $newerr=1; @list = $sftp->glob("$filemask") or $newerr=1; } $i++;

Thank you for your time and help!

Replies are listed 'Best First'.
Re: How to retrieve files at least 5 minutes old using sftp foreign
by atcroft (Abbot) on Nov 06, 2013 at 18:12 UTC

    Take a look at Net::SFTP::Foreign::Attributes (included in Net::SFTP::Foreign)-it includes methods for looking at the atime and mtime of the files. Depending on if the machines' clocks are close, then something like the following (untested) snippet might do the trick (derived from the docs):

    my $t = time; foreach my $f (@list) { if ( $f->{mtime} + 300 < $t ) { # retrieve $f->{filename} here } }

    Hope that helps.

Re: How to retrieve files at least 5 minutes old using sftp foreign
by jellisii2 (Hermit) on Nov 06, 2013 at 18:31 UTC
Re: How to retrieve files at least 5 minutes old using sftp foreign
by salva (Canon) on Nov 07, 2013 at 08:15 UTC
    my $until = time - 5 * 60; $sftp->mget($filemask, $dest, wanted => sub { $_[1]->{a}->mtime < $until });