vonman has asked for the wisdom of the Perl Monks concerning the following question:

I need to create an array of filenames that will store all files that have been created or modified from a given time? (The window will most likely be per day but could also be a specific window including multple days) Does anyone have a quick subroutine that will accomplish this? Thanks Rich
  • Comment on How can I create an array of filenames?

Replies are listed 'Best First'.
Re: How can I create an array of filenames?
by arturo (Vicar) on Mar 08, 2001 at 20:39 UTC
    opendir DIR, "$path" or die "Ack! Thpt! : $!\n"; my @list = grep { -M "$path/$_" <=1 } readdir DIR; closedir DIR;

    The -M filetest returns the time since the file was last modified in days.

    note I hadda modify this after I noticed that it was originally returning files that *hadn't* been modified in over a day.

    danger's answer picked up on that =)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: How can I create an array of filenames?
by danger (Priest) on Mar 08, 2001 at 20:59 UTC

    Several people have already mentioned a few ways to grab files and use the -M test for modification times -- However, you might want to consider using the File::Find module for this, as it will take care of recursively searching a given directory for you and some of the other menial labour involved. The following will give you all the files under the './misc' directory that have been modified within the last day (relative to script start time):

    #!/usr/bin/perl -w use strict; use File::Find; my @files; find(sub{ push @files, $File::Find::name if -M $_ < 1; }, './misc'); print join "\n", @files;

    See the docs on File::Find for further information.

Re: How can I create an array of filenames?
by azatoth (Curate) on Mar 08, 2001 at 20:32 UTC
Re: How can I create an array of filenames?
by jeroenes (Priest) on Mar 08, 2001 at 22:02 UTC
    In quest for the shortest answer:
    @list = grep{ (-M) <= 1} <*>;
    To additionally recurse the directories:
    our @list; findrecent('*'); sub findrecent{ my $glob = shift; for ( glob $glob ){ push( @list, $_) if (-M) <= 1; findrecent( $_.'/*' ) if -d; } }
    Hope this helps,

    Jeroen
    "We are not alone"(FZ)

    Update: You may want to throw in an and not -l to prevent endless loops ;-) thanks to tilly for pointing me to this problem

Re: How can I create an array of filenames?
by Jouke (Curate) on Mar 08, 2001 at 20:32 UTC
    I guess you'll have to look into the readdir function to read the contents of a directory and into the stat function to determine the creation/modification dates. After that you can easily build your array...

    Jouke Visser, Perl 'Adept'