Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^5: Reading every file in a directory

by Athanasius (Monsignor)
on Sep 29, 2012 at 04:27 UTC ( #996332=note: print w/ replies, xml ) Need Help??

in reply to Re^4: Reading every file in a directory
in thread Reading every file in a directory

Hello spookyjack123, and welcome to the Monastery!

I think the following (untested) code will do what you want:

#! perl use strict; use warnings; my $current_time = time; my $old_time = $current_time - (60 * 60 * 24 * 5); printf "Current time is %d, old time is %d\n", $current_time, $old_tim +e; my $directory = '/home/gac3/plugins/Essentials/userdata'; my @to_delete; for my $YMLfile (<"$directory/*.yml">) { open(my $fh, '<', $YMLfile) or die "Cannot open file '$YMLfile' fo +r reading: $!"; while (<$fh>) { if (/ ^ \s* login: \s+ ( \d+ ) $ /x) { push @to_delete, $YMLfile if int($1 / 1_000) < $old_time; last; } } close $fh or die "Cannot close file '$YMLfile': +$!"; } unlink @to_delete;

Note that the timestamps in your sample data are 3 digits longer than the output of Perl’s time function. I have simply truncated the timestamp; you may need to adjust this part of the script.

Hope that helps,

Update: Fixed the call to unlink. Thanks to Kenosis for the heads-up!

Athanasius <°(((><contra mundum

Comment on Re^5: Reading every file in a directory
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-12-25 20:09 GMT
Find Nodes?
    Voting Booth?

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

    Results (163 votes), past polls