mehul79 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to output the file to a new directory ( the files to be mo +dified is in another folder) but iam getting the error cannot open directory My code : #!/usr/local/bin/perl -w # use strict; # use warnings; if($#ARGV != 1) { print "usage: perl directory.pl source direcoty detination direct +ory \n"; exit; } chomp(@ARGV); #my $dirname = "lab"; #my $output="lab2"; my $dirname = $ARGV[0]; my $output= $ARGV[1]; $dirname =~ s/\/$//g; $output =~ s/\/$//g; mkdir $output if (-d $output ==0); #`rm $output/*.lab`; opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { $file2=$file; $input=$dirname."/".$file; $out=$output."/".$file2; print $file."\t".$input."\t".$out."\n"; open(F,"<$input") or die " Cannot open file $input: $!"; open(F1,">$out") or die " Cannot open file $out: $!"; <F>; #while ($line = readline(F)) { #$line=~s/^(.*)\n$/$1/s; #$line=~ s/\r//gi; #print F1 $line ; #} close (F); close (F1); } closedir(DIR);

Original content restored by GrandFather

Replies are listed 'Best First'.
Re: problem in outputting files to a new directory
by GrandFather (Saint) on Aug 27, 2009 at 04:16 UTC

    Is it related to creating a directory using the string in $output, but trying to open a directory using the string in $dirname?

    BTW, don't comment out strictures - ever!

    Do use the three parameter version of open and use lexical file handles:

    open my $inputFile1, '<', $input or die " Cannot open file $input: $!" +;

    True laziness is hard work
Re: problem in outputting files to a new directory
by jwkrahn (Abbot) on Aug 27, 2009 at 05:51 UTC
    #!/usr/local/bin/perl -w # use strict; # use warnings;

    That should be:

    #!/usr/local/bin/perl use strict; use warnings;

    if($#ARGV != 1)

    That is usually written as:

    if(@ARGV != 2)

    chomp(@ARGV);

    There is no need to chomp the @ARGV array!


    $dirname =~ s/\/$//g; $output =~ s/\/$//g;

    You are anchoring the pattern to the end of the string so the /g option is superfluous.


    mkdir $output if (-d $output ==0);

    That would usually be written as:

    mkdir $output or die "mkdir $output: $!" unless -d $output;

Re: problem in outputting files to a new directory
by BioLion (Curate) on Aug 27, 2009 at 08:28 UTC

    Huh? Where has the OP code gone?

    Just a something something...
      Original question (code tags fixed):

      I am trying to output the file to a new directory ( the files to be modified is in another folder) but i am getting the error cannot open directory

      My code:

      #!/usr/local/bin/perl -w # use strict; # use warnings; if($#ARGV != 1) { print "usage: perl directory.pl source direcoty detination direct +ory \n"; exit; } chomp(@ARGV); #my $dirname = "lab"; #my $output="lab2"; my $dirname = $ARGV[0]; my $output= $ARGV[1]; $dirname =~ s/\/$//g; $output =~ s/\/$//g; mkdir $output if (-d $output ==0); #`rm $output/*.lab`; opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { $file2=$file; $input=$dirname."/".$file; $out=$output."/".$file2; print $file."\t".$input."\t".$out."\n"; open(F,"<$input") or die " Cannot open file $input: $!"; open(F1,">$out") or die " Cannot open file $out: $!"; <F>; #while ($line = readline(F)) { #$line=~s/^(.*)\n$/$1/s; #$line=~ s/\r//gi; #print F1 $line ; #} close (F); close (F1); } closedir(DIR);