in reply to Help with ReadDir and Stat?

For a quick solution, without having to rely on (the far superior) modules, I've often used this code (which could certainly be improved). Just pass the starting directory on the command line.
#!/usr/bin/perl use strict; use warnings; open my $LOG, '>', "$0.log"; my $startdir = $ARGV[0] || '.'; push my @dirs, $startdir; while (my $dir = pop @dirs) { opendir DIR, $dir; my @files = grep !/^\.\.?$/, readdir DIR; closedir DIR; foreach my $file (@files) { my $fullname = "$dir/$file"; $fullname = $file if ( $dir eq "/" ); if ( -d $fullname ) { push @dirs, $fullname; next; } if ( -f $fullname ) { if ( $file =~ /\.dat$/i ) { print $LOG $fullname . "\n"; } } } } close $LOG;
You will of course have to add the logic for checking the tile modification time. A shortcut for that is:
my $mtime = stat($file)[9];
Unless, of course you need the other values that stat returns.

Replies are listed 'Best First'.
Re^2: Help with ReadDir and Stat?
by batcater98 (Acolyte) on Mar 13, 2008 at 17:23 UTC
    Thanks, this is a huge help - in relation to the "Date" comparison - get the mod time back using the stat and that works great, but how would I do the comparison to if the file was modified or created on "Today's" date? I.E. if the file was created/modified today I want to keeps it's info - if not I will move on? Having troubles figuring out how to get to teh "Day" comparison. Thanks Again, Ad.
      You can pass the mtime to localtime. i.e.,
      my ($mday, $mon, $year) = localtime($mtime)[3,4,5];
      Then do the same with the current time:
      my ($cmday, $cmon, $cyear) = localtime(time)[3,4,5];
      and then compare them. I'm sure there's a more efficient way, but this should work.
        You have been a great help - I am learning a lot here. One more question. I am working on the date compare and I am getting errors. Here is what I am trying.
        my $lctime = strftime("%m_%d_%y",localtime); my $fltime = strftime("%m_%d_$y",localtime(stat($filename)[9])); if ($lctime = $fltime) { # processs file data }
        The Localtime(mtime) returned from the stat function is giving an error. Ideas on this? Thanks, Ad.