cs87
cs87

Reputation: 37

perl how to read multiple .txt file in a folder?

I would like to ask how to make the perl script able to run multiple text file in a folder ?

Currently i need to key in the file name at $directory to get the script run . But when i want to run more file i need to always change the $directory file name.

Is there any easier way to get it done if i have 5 files in a same folder. Many thanks.

#!/usr/bin/env perl

use strict;
use warnings 'all';

my $directory = 'C:\Users\abc.txt.log';
my $testna    = 'nomv_deepsleep current';
my $testpin   = 'AFE_PMU_I_VDDLDO_1P8_EXT';

my @header = (
    'Unit#',        'Test_Name', 'Pin_Name', 'Low_limit',
    'Measure_Data', 'High_limit'
);
my $format = "%-8s %-30s %-40s %-15s %-15s %-s\n";
my $unit;

my $outfile = "$directory.sdc";

open( OUT, "> $outfile" );
open( INF, "$directory" )
    || die("\n ERROR, cannot open input file called: $directory\n ($!)\n\n");

printf $format, @header;
printf OUT $format, @header;

while (<INF>) {

    if (/Device#:\s*(\d+)/) {
        $unit = $1;
        next;
    }

    chomp;

    my @fields = split /\s{2,}/;

    if ( $fields[2] eq $testna and $fields[3] eq $testpin ) { # TEMP_SENSE_VBE

        printf $format, $unit, $fields[2], $fields[3], $fields[5],
            $fields[6], $fields[7];
        printf OUT $format, $unit, $fields[2], $fields[3], $fields[5],
            $fields[6], $fields[7];
    }

}
close(INF) || die "cannot close input file !!";
close(OUT);

Upvotes: 0

Views: 637

Answers (2)

Borodin
Borodin

Reputation: 126722

Just put the code into a subroutine and call it for each value returned by glob

This code expects the directory path as a parameter on the command line, and processes all .log files found there

For instance, you would run

perl myprog.pl C:\Users

to find and process C:\Users\abc.txt.log and any other files ending in .log

use strict;
use warnings 'all';
no warnings 'qw';

use File::Spec::Functions 'catfile';

use constant TESTNA  => 'nomv_deepsleep current';
use constant TESTPIN => 'AFE_PMU_I_VDDLDO_1P8_EXT';

my ($directory) = @ARGV;

process_file($_) for glob catfile($directory, '*.log');

my @header = qw/ Unit#  Test_Name  Pin_Name  Low_limit  Measure_Data  High_limit /;
my $format = "%-8s %-30s %-40s %-15s %-15s %-s\n";


sub process_file {
    my ($log_file) = @_;
    my $outfile = "$log_file.sdc";

    open my $inp_fh, '<', $log_file or die qq{\n ERROR, cannot open "$log_file" for input: $!\n\n};
    open my $out_fh, '>', $outfile  or die qq{\n ERROR, cannot open "$outfile" for output: $!\n\n};

    printf STDOUT  $format, @header;
    printf $out_fh $format, @header;

    my $unit;

    while ( <$inp_fh> ) {

        if (/Device#:\s*(\d+)/) {
            $unit = $1;
            next;
        }

        chomp;

        my @fields = split /\s{2,}/;

        if ( $fields[2] eq TESTNA and $fields[3] eq TESTPIN ) {    # TEMP_SENSE_VBE

            printf STDOUT  $format, $unit, @fields[ 2, 3, 5, 6, 7 ];
            printf $out_fh $format, $unit, @fields[ 2, 3, 5, 6, 7 ];
        }
    }
}

Upvotes: 1

mkHun
mkHun

Reputation: 5927

Try to use glob

 while (my $directory = glob "C:/Users/*.txt")
{
   .. 
   ..

}

Upvotes: 2

Related Questions