Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^5: Reading every file in a directory

by Athanasius (Chancellor)
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996332]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2018-05-24 18:37 GMT
Find Nodes?
    Voting Booth?