mahesh mahesh -3 years ago 55
Perl Question

Error while trying to search for specific file with in while loop

How do I search for a specific jar file within a while loop? In the code below,

$application
,
$location
,
$version
are command line arguments

sub do_verify
{
while($application eq 'abc')
{
my $dir = "/viewstore/ccabc/dst_${application}_${version}/abcportal/${location}/target";
opendir(DIR, $dir) or die $!;
my $jarfile;
next unless ($jarfile =~ /\.jar$/);
print "$jarfile\n";
do_upload();
}
}

Answer Source

The following code should work for you. Currently you are not iterating the files inside your specified directory.

sub do_verify {

    while($application eq 'abc') {

        my $dir = "/viewstore/ccabc/dst_${application}_${version}/abcportal/${location}/target";

        opendir(DIR, $dir) or die $!;

        my @jarfiles = grep { (/\.jar$/) && -f "$dir/$_" } readdir(DIR);

        foreach (@jarfiles) {

            my $jarfile = $_;

            print "$jarfile\n";

            do_upload();
        }

    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download