in reply to Unable to read a file

thanks rovf I got the issue by printing $!
#!/usr/bin/perl use warnings; use strict; print "Enter the path where the log file is placed : "; my $dir =<>; chomp($dir); opendir DIR, $dir or die "cannot open dir $dir: $!"; my @files= readdir DIR; my $len=scalar(@files); print $len; print "Enter the path where to save the report"; my $dest_dir = <STDIN>; chomp($dest_dir); open(FH1,">$dir./result.txt") if( $len > 0); foreach my $file (@files){ if($file=~/\.txt/){ print "$file\n"; open(FH,"$dest_dir./$file") or die $!; while(my $line= <FH> ){ print FH1 $line; } close FH; } } close FH1; closedir DIR;
Any other suggestions or feedbacks on this are welcome. Thanks everyone.

Replies are listed 'Best First'.
Re^2: Unable to read a file
by Ken Slater (Initiate) on Jun 13, 2012 at 16:11 UTC

    It appears you changed the line

    open(FH,"$file") or die "Can't read the file\n";

    to

    open(FH,"$dest_dir./$file") or die $!;

    However, you read the files from $dir not $dest_dir, so obviously they will not be found.

    Also, you do not need the period/dot:

    open(FH,"$dir/$file") or die $!;

    As mentioned before, get rid of the period/dot on this line:

    open(FH1,">$dest_dir./result.txt") if( $len > 0);

    Should be

    open(FH1,">$dest_dir/result.txt") if( $len > 0);

    Or better yet, use the three argument form of open:

    open(FH1,'>',"$dest_dir/result.txt") if( $len > 0);

    Of course it would be good if you checked whether the open failed or not.

    HTH, Ken