Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Range of File Names and For/While Problem

by svsingh (Priest)
on Sep 22, 2003 at 19:47 UTC ( #293253=note: print w/replies, xml ) Need Help??

in reply to Range of File Names and For/While Problem

You may want to try globbing to build your list.
use File::Glob qw(:globally :nocase); chdir 'd:\log\exported'; my @files = <*.elog>;
Alternatively, if you want to do processing on each file in the list, File::Find will also work for you and would give you a little more control over which files make it into the list.

Replies are listed 'Best First'.
Re: Re: Range of File Names and For/While Problem
by svsingh (Priest) on Sep 22, 2003 at 20:41 UTC
    I thought I'd take a quick break from work and see if I could write the File::Find version of this. The following code will create a list of the files modified in the past seven days in d:\log\exported. I didn't backdate the files in my directory to test this thoroughly, but it seems to work.
    use File::Find; my $ageLimit = 7; # the age (in days) of the oldest file you want to +process my $ageCutoff = time - $ageLimit * (60*60*24); # the cutoff time my @logFiles = (); my $findFilter = sub { if ( (stat($_))[9] > $ageCutoff ) { # only push files newer than c +utoff push @logFiles, $_; } return; }; find($findFilter, 'd:\log\exported'); # populates the list

      Dont forget File::Find is recursive. So youll grab all the files that match in sub directories as well, and should it contain a directory thats new enough you'll have a problem as it'll get included as well. Also as you are simply pushing the base name into the list you wont even know whats happening until you start to diagnose missing files and find them two layers deeper in the file structure :-)

      use File::Find; my @files; my $age_limit=7; find { wanted=>sub { -f and -M($_) < $age_limit and push @files, $_ }, no_chdir=>1 }, 'd:\log\exported'; print join("\n",@files),"\n";

      You should take a look at find2perl


      Everybody remember the Gandhi quote?

        First they ignore you, then they laugh at you, then they fight you, then you win.

      Gentlemen and ladies, this newest leaked memo from Microsoft confirms that we are advancing through GandhiCon Three.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2021-04-18 16:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found