in reply to Re: Opening multiple log files
in thread Opening multiple log files

Thank you Ken. I've corrected the errors as suggested and confirmed that it is writing to the correct path name which is the current path.

I'm trying to find out how can I execute this exact same script for all of the files(many) in the same directory. The files that I want to include are those with the extension of '.log'. To be specific, the files are log files and I am trying to capture data(succeeded) from each of the file. I have managed to do it for one file now I am trying to do it for each file with a the `.log` extension in the current folder.

Replies are listed 'Best First'.
Re^3: Opening multiple log files
by kcott (Archbishop) on Jun 15, 2015 at 07:47 UTC

    The builtin glob function will probably do what you want. Just make sure you check the "Portability issues" noted in that document.

    Something like:

    my @logfiles = glob "*.log";

    then just iterate @logfiles, processing each in turn.

    Because you said "... all of the files(many) in the same directory ..." you may exceed a maximum size. See GLOB_LIMIT in File::Glob for details.

    If you are likely to approach that limit, a better option (although, a bit more coding) would be to use readdir.

    You'll need to skip everything except normal files with a .log extension. Something like this (untested):

    while(readdir $dh) { next unless -f and /\.log$/; # Process log file here }

    See File Test Operators if you're unfamiliar with -f.

    -- Ken

      I see. Alright, I won't be using modules cause I'm still learning so I want to do more of the coding first. I understand the code you have provided would read every file in the directory which have the .log extension.

      However, is there anyway anyway to get the file name as well? If you notice at the second part of my code I am trying to get the file that I have opened(which is formatted in a timestamp)to use it to print in the file I'm going to create. I'm not sure how to do that with your codes.

        "... I won't be using modules ..."

        I not certain what you're getting at here. The link to File::Glob was for documentation to read, not a module to use. The link to glob() explains this. Besides, you're already using three modules:

        use strict; use warnings; use POSIX 'strftime';
        "... is there anyway anyway to get the file name as well?"

        The filename that -f operates on? The filename that /\.log$/ is attempting match?

        -- Ken

        I see. Alright, I won't be using modules cause I'm still learning so I want to do more of the coding first.

        The most important thing you can do is start creating subroutines , your gets shorter the more you do this

        { my %details; for my $infile( @inputFiles ){ FillDetails( \%details, $infile ); } SpillDetails( \%details, $outfile ); } exit( 0 );