#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    82
    Rep Power
    3

    Error-No such file or directory


    Hi,

    I just found my script doesn’t want to work on a machine with Perl 5.8 installed.
    I used Perl 5.12 to write the code and it works on my PC.
    Getting error: “Cannot open file … No such file or directory…”

    Basically I’m looking for new files in a DIR.
    I’ll process each new file and then I’ll move the files to a network DIR under the same names.

    Do not know how to get around it. Any help will be appreciated.
    Thanks, tester


    Code:

    Code:
     #!/use/bin/perl -w
     use strict;
     use warnings ;
    
         my $compname = $ENV{COMPUTERNAME} ;
    
              use File::Path;
               unless(-d "Z:/TErrors/$compname")              
             {    mkpath("Z:/TErrors/$compname", 0, 0755); }
        
    
         my $dir = 'D:/Temp';
         opendir(DIR, $dir) or die "Can't open directory $dir $!\n";
         
            while (my $file = readdir(DIR)) {
             next unless (-f "$dir/$file");
             next unless (-M "$dir/$file" < 1); 
             next unless ($file =~ m/\.log$/); 
             print $file, "\n" ;
    
    
     my  $logname   = $file ;  
         $nf = "Z:/TErrors/$compname/$logname" ; 
         
        open my $nf_fh,'>',$nf or die "Can't open $nf $!\n" ;
        
        open my $file_fh,'<',$file or die "Can't open $file  $!\n " ;
        
              while (my $line =<$file_fh>) {
                      chomp ($line);              
        	 if ($line =~ /SomeMatch/) 
        	 { 
        	  # do somehting else #
                print $nf_fh, $line "\n" ;	 
             }
           }
          close $file_fh ;
          close $nf_fh ;
          exit ;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Where do you get the error (i.e. on opening the input or the output file)?

    As a side note, if your code was properly indented, you would have discovered that you are not closing your filehandles at the right place (you should do it once for every open statement, instead of once at the end), but that should still work because Perl is very nice with people doing this kind of mistakes. But you might discover some other mistake that I did not see. Indenting properly is never a waste of time.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Your script won't compile.

    The starting point would be to read and then fix the problems that those error messages point out.

    c:\testing>perl -c testerV.pl
    String found where operator expected at testerV.pl line 34, near "$line "\n""
    (Missing operator before "\n"?)
    Global symbol "$nf" requires explicit package name at testerV.pl line 23.
    Global symbol "$nf" requires explicit package name at testerV.pl line 25.
    Global symbol "$nf" requires explicit package name at testerV.pl line 25.
    syntax error at testerV.pl line 34, near "$line "\n""
    Missing right curly or square bracket at testerV.pl line 39, at end of line
    syntax error at testerV.pl line 39, at EOF
    testerV.pl had compilation errors.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    82
    Rep Power
    3
    Thank you Lauren, Fish!
    I fixed it by adding lines (used code Fish suggested in my previous post):

    Code:
       my @logfile = grep( -M $_ < 1, <$dir/*.log>);
       
       	  foreach my $file ( @logfile ) {
       	  	  chomp $file;
       	    	  print $file, "\n" ;	  
       	      my @logname  = split ("\/",$file) ;
       	  	  print $logname[-1], "\n" ;
    
        open my $file_fh,'<', $file or die "CAN'T open $file $!\n" ;      
    
        my $nf = "Z:/TErrors/$compname/$logname[-1]" ;
        open my $nf_fh,'>',$nf or die "Can't open $nf $!\n" ;
    And here:

    Code:
         close $file_fh ;
         close $nf_fh ;
           }
          exit ;
    Thank you again!
    testerV

IMN logo majestic logo threadwatch logo seochat tools logo