CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Join files in a directory code errors out

 



tester_V
Novice

Nov 20, 2017, 10:17 PM

Post #1 of 7 (1720 views)
Join files in a directory code errors out Can't Post

Greetings!
I thought it would be easy task but for some reason my code produces an error. I'm trying to join files in a directory, a lot of files.
Any help appreciated!
Thank you all in advance!

Here is an error I have:

Uncaught exception from user code:
Can't open file...

Here is the code:

#!/usr/local/bin/perl
use strict ;
use warnings ;
use diagnostics ;
use File::Copy ;

my $dir22 = "C:/SixShot_OUT/" ;
opendir(DH, $dir22);
my @files22 = readdir(DH);
closedir(DH);

my $joined_flow_files = "C:/Sixshot_logs/Joined-Flow-logs.txt" ;
open (my $joined_flow_files_fh,'>',$joined_flow_files) or die "Cannot open file $!" ;

foreach my $files22 (@files22)
{
next if($files22 =~ /^\.$/);
next if($files22 =~ /^\.\.$/);
chomp $files22 ;
print "$files22 \n" ;

open (my $files22_fh,'<',$files22) or die "Can't open $files22 file from directory" ;
while (<$files22_fh>)

{
print "$_ \n" ;
}
}

close $joined_flow_files_fh ;

Code



      
    


Laurent_R
Veteran / Moderator

Nov 21, 2017, 1:25 AM

Post #2 of 7 (1719 views)
Re: [tester_V] Join files in a directory code errors out [In reply to] Can't Post

Hi,
does the program print out anything before that error message? If yes, please say so.

I don't see any obvious error in your code, but there are things that you could do to improve the error message that you receive and to help diagnosing the problem.

Make sure you find the input directory or tell that you can't open it and print out the error returned by the system:

Code
opendir(DH, $dir22) or die "unable to open $dir22 $!";


Further down, print out which file couldn't be opened:

Code
open (my $joined_flow_files_fh,'>',$joined_flow_files) or die "Cannot open file $joined_flow_files $!" ;

Again further down, get as much information as possible:

Code
open (my $files22_fh,'<',$files22) or die "Can't open $files22 file from directory $!" ;

With these three changes, you should be able to figure out which of these three lines failed and why it failed. Then it should be much easier to find the underlying reason (such as, perhaps, a mistake in the directory name or something else).

Just another comment:

Code
foreach my $files22 (@files22) 
{

Having the same name for an array and for its individual items is fairly confusing for you and me (even though the computer should be able to figure out). Also, the loop variable represents only one file at a time, use a singular name, it makes the intent clearer. So, it would be better to have something like this:

Code
foreach my $file (@files22) 
{

(and change $files_22 to $file in the rest of the foreach loop.


FishMonger
Veteran / Moderator

Nov 21, 2017, 11:23 AM

Post #3 of 7 (1713 views)
Re: [tester_V] Join files in a directory code errors out [In reply to] Can't Post

@files22 will hold the filenames but not the path. When you want to open the filehandle, you need to add the path otherwise it's expecting it to be in the cwd, which it isn't.


Laurent_R
Veteran / Moderator

Nov 21, 2017, 2:33 PM

Post #4 of 7 (1709 views)
Re: [FishMonger] Join files in a directory code errors out [In reply to] Can't Post

Right, I missed that error. BTW, this is one of the reasons I usually much prefer to use the glob function, rather than opendir/readdir, when I can.

As a side note, there is another slight mistake in the OP which I noticed before but forgot to mention in my other post:


Code
while (<$files22_fh>) 
{
print "$_ \n" ;
}


There is no need to add an end of line character here, since it will be already there in the input lines.


FishMonger
Veteran / Moderator

Nov 21, 2017, 2:47 PM

Post #5 of 7 (1704 views)
Re: [Laurent_R] Join files in a directory code errors out [In reply to] Can't Post

And that while loop could be shortened/simplified to this:

Code
print <$files22_fh>;


But I suspect that the OP meant to write it to the $joined_flow_files_fh filehandle.

Code
print $joined_flow_files_fh <$files22_fh>;



Laurent_R
Veteran / Moderator

Nov 21, 2017, 3:41 PM

Post #6 of 7 (1700 views)
Re: [FishMonger] Join files in a directory code errors out [In reply to] Can't Post

Yes, and most probably yes.


tester_V
Novice

Nov 21, 2017, 5:23 PM

Post #7 of 7 (1695 views)
Re: [Laurent_R] Join files in a directory code errors out [In reply to] Can't Post

Yes, it was the problem the "foreach" lop printed just the files but not the files +patch to files. I cannot believe I did not catch it before I made the postFrown

Thank you Laurent and Fish for the input and for the little education that is equally important and very much appreciated.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives