Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: Reading every file in a directory

by spookyjack123 (Initiate)
on Sep 29, 2012 at 03:12 UTC ( #996327=note: print w/ replies, xml ) Need Help??


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

Oh 1 note I just discovered, I believe the sample here that you posted:

use strict; use warnings; my $directory = '.'; for my $YMLfile (<"$directory/*.yml">) { open my $fh, '<', $YMLfile or die $!; while (<$fh>) { # process the file's contents } close $fh; }

That code is good for opening the directory, I'm super grateful :) ! now reading and parsing the files is the next main issue for this script, the login line in the comment I last posted needs to be assigned to a value that can be read, then the login: followed by a space, need to be removed so it only shows the timestamp (the parsing part) then comparing will be super easy. Any ideas there ?


Comment on Re^4: Reading every file in a directory
Download Code
Replies are listed 'Best First'.
Re^5: Reading every file in a directory
by Athanasius (Abbot) on Sep 29, 2012 at 04:27 UTC

    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

Re^5: Reading every file in a directory
by Kenosis (Priest) on Sep 29, 2012 at 04:44 UTC

    Try the following on a scratch directory that contains some scratch .yml files:

    use strict; use warnings; my $directory = './yml'; my $fiveDays = 5 * 86400; my $currentTime = time; my ( $fileLine, $timeStamp ); for my $YMLfile (<"$directory/*.yml">) { open my $fh, '<', $YMLfile or die $!; while ( $fileLine = <$fh> ) { last if ($timeStamp) = $fileLine =~ /login: (\d+)/; } close $fh; # Divide 13 digit TS by 1000 to convert to 10 digit TS if ( ( $currentTime - $timeStamp / 1000 ) > $fiveDays ) { print "Login TS in $YMLfile is greater than five days old.\n"; } }

    Verify that the files shown by the routine have a login timestamp (TS) greater than five days. If the script is working as needed/expected, try the following on your scratch dir and files (place it on the next line after the print statement, so files are conditionally unlinked):

    unlink $YMLfile or warn "Could not unlink $YMLfile: $!";

    Disclaimer: Be very careful with this file-deleting script, as you're assuming all responsibility for the use of it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2015-07-08 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls