Re: problem printing contents of file in directory



alok nath wrote:
Hi,

Hello,

Can anybody tell me why its not printing the contents of each
inside a particular folder ?

Yes I can, and so can Perl's documentation:

perldoc -f readdir


my $tstToRunDir = "C:\\perlScripts" ;
my $fileInTstToRunDir ;

opendir TST2RUN, $tstToRunDir || die "Failed to open $tstToRunDir $!\n" ;
open RESULTS_LOG, ">>ResultLog.log" || die "Failed to open log\n" ;

The logical or operator '||' has relatively high precedence so neither opendir nor open will die if an error is encountered. You need to either use parentheses:

opendir( TST2RUN, $tstToRunDir ) || die "Failed to open $tstToRunDir $!\n" ;
open( RESULTS_LOG, ">>ResultLog.log" ) || die "Failed to open log\n" ;

or use the low precedence 'or' operator:

opendir TST2RUN, $tstToRunDir or die "Failed to open $tstToRunDir $!\n" ;
open RESULTS_LOG, ">>ResultLog.log" or die "Failed to open log\n" ;


while ($fileInTstToRunDir = readdir (TST2RUN)){
chomp $fileInTstToRunDir ;

$fileInTstToRunDir comes directly from the file system so there is no reason to use chomp.


#open the file and get connection description and test description
open FILE_2RUN, $fileInTstToRunDir || die " Failed to open $fileInTstToRunDir:$!\n" ;

Again, there is a precedence problem with the '||' operator. The file name in $fileInTstToRunDir was obtained from the $tstToRunDir directory so you need to include the directory name in order to access it:

open FILE_2RUN, "$tstToRunDir/$fileInTstToRunDir"
or die " Failed to open $tstToRunDir/$fileInTstToRunDir:$!\n" ;


print "File is : $fileInTstToRunDir\n" ;
next if($fileInTstToRunDir =~ m/^./ ) ;

while( <FILE_2RUN> ){
print $_ ;
if ($_ =~ m/<Test Description/ ){
print $_ ;
}
} print RESULTS_LOG "File is : $fileInTstToRunDir\n" ;
#$count++ ;
# close FILE_2RUN ; }
close RESULTS_LOG ;
close TST2RUN ;


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.