in reply to Re: Read Directory and getlines in .csv
in thread Read Directory and getlines in .csv

I apologize, please be patient with me. I do not really understand your response. These files are on a server and all are not uploaded daily, depends on the activity (no set pattern). Sometimes 2 other times 14 csv files. Therefore, I do not know the filenames/unknown_path_in_that_case of the files which were modified that day (today) until I run the program I created. But, I do want to read each filename/unknown_path.. to getlines from that .csv place result in one document (a .csv) then go to next file, recursively going through files until end of today's modified files.

  • Comment on Re^2: Read Directory and getlines in .csv

Replies are listed 'Best First'.
Re^3: Read Directory and getlines in .csv
by poj (Abbot) on Apr 27, 2016 at 17:13 UTC

    Not sure what part you are having trouble with but try this

    #!/usr/bin/perl use strict; use warnings; use File::stat; use Text::CSV_XS; my $csv = Text::CSV_XS->new( { binary => 1, eol => $/ } ); my $ONE_DAY = 86400; my $Foutput = "data0426-2.csv"; open my $out, ">", $Foutput or die "$!"; # create array of csv filenames in directory my $dirname = "Daily_QoS_CPD_link"; unless (-d $dirname) { die "Not able to open $dirname"; } my @dir = glob "$dirname/*.csv"; # parse files modifed in past 24 hours foreach my $infile (@dir){ my $age = time()-stat($infile)->mtime; if( $age < $ONE_DAY ){ open my $FH, "<", $infile or die "Could not open $infile : $!"; while (my $row = $csv->getline($FH)) { if ( $row->[2] =~ /^(DROPPED-10|CALL_START|CALL_END)$/ ) { $csv->print($out, $row); } } close $FH; } }
    poj

      Works beautifully. I think I need to look up a few things to see how/why/what of them like using "glob" and "unless. Whew! Most of the components you have in your code are what I had in mine. I was close, but, alas...no cigar. (sad face) BUT->I'm learning!! Thank you so much for your assistance. I kinda like Perl, its very powerful.

        glob does more or less the same thing as opendir /readdir, except that it is more practical: first, it's one line instead or two, second it removes pseudo-directories such as "." and ".." which you usually don't want to explore, and finally it returns a list of files with their path, so that you can open them without having to add the path.

        unless does exactly what the English meaning of the word implies. It is essentially equivalent to if not. For example:

        my $q = $c / $d unless $d == 0; # avoid division by zero error