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
In reply to Re^5: Reading every file in a directory
by Athanasius
in thread Reading every file in a directory
by spookyjack123
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |