in reply to Read Directory and getlines in .csv

I realized I am checking for modified date twice. I have corrected my code.

#!/usr/bin/perl use strict; use warnings; use File::stat; use Text::CSV_XS; use IO::File; my $dirname = "Daily_QoS_CPD_link"; opendir(DIR, $dirname) or die "Not able to open $dirname $!"; # Create an array of files in directory my @dir = grep /\.csv/, $dirname ne '.' && $dirname ne '..', readdi +r DIR; rewinddir(DIR); # create a list of csv's for today into a text file. my $One_day = 86400; foreach my $list (@dir){ my $diff = time()-stat("$dirname/$list")->mtime; if( $One_day > $diff){ open FILE, ">>CPD_Files.txt" or die "Not able to open f +ile $!"; print FILE "$list\n"; } } closedir DIR; close FILE;

Replies are listed 'Best First'.
Re^2: Read Directory and getlines in .csv
by Cristoforo (Curate) on Apr 27, 2016 at 16:45 UTC
    You could avoid the use of File::stat by checking for csv files less than a day old with

    my @today = grep /\.csv$/ && -M "$dir/$_" < 1, readdir DIR;

    (See file test ops).

      To avoid confusion: that works, but -M also stats the file, and does not return the same thing as the mtime field.

      I have used this site to learn many things. I thought I read that -mtime was more consistent than -M?