I am trying to open multiple log files that are in a directory. So far what I've done is
#!/usr/bin/perl use strict; use warnings; use POSIX 'strftime'; #my $filename = 'IGXLEventLog.3.17.2015.20.25.12.625.log'; my $directory = "/opt/lampp/htdocs/otpms/Data"; opendir (my $dir, $directory) or die "Could not open directory '$direc +tory': $!"; my @list_files; our $output; my %details; for my $filename (@list_files) { open(my $fn, '<', $filename) or die "Could not open file '$filenam +e': $!"; while(my $row = <$fn>) { chomp $row; if ($row =~ /Computer Name:\s*(\S+)/i ) # match computer name +with white space then non white space { $details{tester_name} = $1; } elsif ($row =~ /Operating System:\s*(.*\S)/i ) # match operati +ng system with white space then any word { $details{op_sys} = $1; } elsif ($row =~ /IG-XL Version:\s*([^;]*)/i ) # match ig-xl ver +sion with white space then semi colon { $details{igxl_vn} = $1; } elsif ($row =~ /^([\d.]+)\s+(\S+)(?=\s)/ ) #match slot with wh +ite space and then non white space { push @{$details{slot}}, $1; push @{$details{board_name}}, $2; } } close ($fn); my $timestamp = strftime '%Y-%m-%d.%H:%M:%S', localtime; $output = $timestamp .'.sql'; open(my $fh, '>', $output) or die "Could not create file '@output': $! +"; my $log_time_stamp = (stat($filename))[9]; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime +($log_time_stamp); my $nice_timestamp = sprintf ( "%04d-%02d-%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min, +$sec); print $nice_timestamp; for (my $i = 0; $i < @{$details{slot}}; $i++) { print {$fh} "INSERT INTO TesterDeviceMatrix.TBL_TESTER_INFO" ."(tester_name, operating_system, version, board_name, config +, date_modified, log_created) " ."VALUES ('$details{tester_name}', '$details{op_sys}', '$detai +ls{board_name}[$i]', " ."'$details{igxl_vn}', '$details{slot}[$i]', '$timestamp', '$n +ice_timestamp');\n"; } close($fh); }
From this I am getting nothing. The basic flow of this program (originally) is to capture the data needed in a log file and create a new file with a .sql extension. However, after adding the @list_files array and the for loop it doesn't seem to create any files. Any help here? I've searched for solutions and I'd prefer the ones that use the open or opendir instead of modules and the other ways.

EDIT 2 I have decided to use readdir as suggested by one of the anonymous monks however, I am getting only one file and all the info captured from other files is combined into this one. Any help?:

This is the working code for one file only, with the name hard coded
#!/usr/bin/perl use strict; use warnings; use POSIX 'strftime'; our $output; my %details; #my $filename = 'IGXLEventLog.3.17.2015.20.25.12.625.log'; my $directory = "/opt/lampp/htdocs/otpms/Data"; opendir (DIR, $directory) or die "Could not open directory '$directory +': $!"; my @files = readdir(DIR); closedir DIR; foreach my $filename (@files) { open(my $fn, '<', "$directory/$filename") or die "Could not open file +'$filename': $!"; while(my $row = <$fn>) { chomp $row; if ($row =~ /Computer Name:\s*(\S+)/i ) # match computer name +with white space then non white space { $details{tester_name} = $1; } elsif ($row =~ /Operating System:\s*(.*\S)/i ) # match operati +ng system with white space then any word { $details{op_sys} = $1; } elsif ($row =~ /IG-XL Version:\s*([^;]*)/i ) # match ig-xl ver +sion with white space then semi colon { $details{igxl_vn} = $1; } elsif ($row =~ /^([\d.]+)\s+(\S+)(?=\s)/ ) #match slot with wh +ite space and then non white space { push @{$details{slot}}, $1; push @{$details{board_name}}, $2; } } close ($fn); my $timestamp = strftime '%Y-%m-%d.%H:%M:%S', localtime; $output = $timestamp .'.sql'; open(my $fh, '>', "$directory/$output") or die "Could not create file +'$output': $!"; my $log_time_stamp = (stat($filename))[9]; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime +($log_time_stamp); my $nice_timestamp = sprintf ( "%04d-%02d-%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min, +$sec); print $nice_timestamp; for (my $i = 0; $i < @{$details{slot}}; $i++) { print {$fh} "INSERT INTO TesterDeviceMatrix.TBL_TESTER_INFO" ."(tester_name, operating_system, version, board_name, config +, date_modified, log_created) " ."VALUES ('$details{tester_name}', '$details{op_sys}', '$detai +ls{board_name}[$i]', " ."'$details{igxl_vn}', '$details{slot}[$i]', '$timestamp', '$n +ice_timestamp');\n"; } close($fh); }

In reply to Opening multiple log files by hahazeeq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.