Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
P is for Practical
 
PerlMonks  

Reading every file in a directory

by spookyjack123 (Initiate)
on Sep 29, 2012 at 00:44 UTC ( #996309=perlquestion: print w/ replies, xml ) Need Help??
spookyjack123 has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that will check the age of over 20,000 YML files that contain individual timestamps, and delete ones of old age, despite being fairly new to perl, I was able to create a timestamp comparision bit of code to check the age

# Current Time Value my $CurrentTime = `perl -e 'print time, '`; print("$CurrentTime\n"); # 432000 Second in 5 days my $OldTime = "$CurrentTime" - 432000; print("$OldTime\n");

This code works without issue, however I have spent a while looking for a solution to read the mass of thousands of files in the directory /home/gac3/plugins/Essentials/userdata with no luck. my queston to all you knowledgeable and wise experts, how do you assign those files within the directory (all of which are the same format) to be opened so the timestamp may be read ?

Comment on Reading every file in a directory
Download Code
Re: Reading every file in a directory
by Anonymous Monk on Sep 29, 2012 at 01:06 UTC

     my $CurrentTime = `perl -e 'print time, '`;

    Read perlintro the use  my $CurrentTime = time;

Re: Reading every file in a directory
by Anonymous Monk on Sep 29, 2012 at 01:10 UTC

      Every file is the same format (.yml) as well as the same format, I need a system like a glob that is super light for the job, and short. one that opens all files in the directory regardless of format type, just something where a directory is assigned, and it opens the files. Would this serve that, or is there an even simpler method ?

        Here's an option:

        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; }

        Update: Perhaps I've misunderstood your issue. In one place you say that you want the files "...to be opened so the timestamp may be read...," as if the timestamp may be a string within the file. Yet, at the beginning, you mention a script that can "...check the age of over 20,000 YML files..." In the latter case files are not opened. If it's the latter case, the following may assist you:

        use strict; use warnings; use File::stat; my $directory = '.'; my $currentTime = time; for my $YMLfile (<"$directory/*.yml">) { my $mtime = stat($YMLfile)->mtime; print "$YMLfile: " . ( $currentTime - $mtime ) . " seconds old\n"; }

        You can also try the -M operator on the files, which returns the file's age in days:

        for my $YMLfile (<"$directory/*.yml">) { print -M $YMLfile, "\n"; }

        And the following PM node may be helpful: How do I find and delete files based on age?.

        I strongly suggest running your file-deleting script on a practice directory.

        Would this serve that, or is there an even simpler method ?

        You tell me? Try It To See?

Re: Reading every file in a directory
by Anonymous Monk on Sep 29, 2012 at 01:51 UTC

    Hi,

    Also look at stat.

    J.C.

Re: Reading every file in a directory
by cLive ;-) (Parson) on Sep 29, 2012 at 06:33 UTC

    I'm really tired and maybe missing something, but is there any reason you can't just run this in the shell?

     find /path/to/dir -mtime +5 -type f|xargs rm

    If the files are only getting updated when the user last logs in then that's a perfectly acceptable solution and doesn't involve you having to parse the files in question.

    (ninja edit - add restrict to files)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-04-19 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls