Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How can one create a folder B in D drive and save results in a text file inside B using perl script?

by supriyoch_2008 (Scribe)
on Dec 10, 2012 at 04:39 UTC ( #1008035=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I am interested to create a folder B in D drive of computer and then save results in a text file (Result_File.txt) inside B folder using perl. For this I have written the following script p5.pl but the cmd has shown an error. I am at my wit's end to find a solution. I searched in perldoc but couldn't find the code applicable in my case. I can't make out what the technical term perlIO layer really means in the error message in cmd. May I request PerlMonks to provide suggestions in this matter?

Here goes the script p5.pl:

#!/usr/bin/perl use warnings; use strict; my $a=9; my $b=5; my $c=$a*$b; ## Create the folder B in D drive & Save results as Result_File.txt # within B folder: my $output="Result_File.txt"; open (my $fh,">D://B/",$output) or die"Cannot open file'$output'.\n"; +# Line 10 print $fh "\n WELCOME! Product of 9 & 5= $c\n\n"; close $output; exit;

Here goes the error message in cmd screen:

C:\Users\x\Desktop>p5.pl Unknown PerlIO layer "D" at C:\Users\x\Desktop\p5.pl line 10. Cannot open file'Result_File.txt'.

Comment on How can one create a folder B in D drive and save results in a text file inside B using perl script?
Select or Download Code
Re: How can one create a folder B in D drive and save results in a text file inside B using perl script?
by Athanasius (Monsignor) on Dec 10, 2012 at 04:58 UTC

    Create the directory first using mkdir, and test the return value. If this is successful, you should then be able to create the file and write to it.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hi Athanasius

      Thanks for your suggestions.

      Regards,

Re: How can one create a folder B in D drive and save results in a text file inside B using perl script?
by LanX (Canon) on Dec 10, 2012 at 05:04 UTC
    not

    open (my $fh,">D://B/",$output)

    Perl tries to parse a IO layer like ":utf8" or ":raw" are.

    Better

    open (my $fh,">", "D://B/$output")

    ( I'm not sure about the slashes under win.)

    You've been already pointed to mkdir and read open again.

    And please do close $fh; not close $output;

    Cheers Rolf

      Hi LanX,

      Thank you very much for constructive suggestions. I shall try once again.

      With Regards,

Re: How can one create a folder B in D drive and save results in a text file inside B using perl script?
by Kenosis (Priest) on Dec 10, 2012 at 05:45 UTC

    Perhaps the following will assist:

    #!/usr/bin/perl use warnings; use strict; my $a = 9; my $b = 5; my $c = $a * $b; my $dir = 'D:/B'; my $output = 'Result_File.txt'; ## Create the folder B in D drive & Save results as Result_File.txt # within B folder: mkdir $dir or die $!; open my $fh, '>', $dir . '/' . $output or die "Cannot open file '$outp +ut'.\n"; # Line 10 print $fh "\n WELCOME! Product of 9 & 5 = $c\n\n"; close $fh;
    • perltidy was used to reformat the script.
    • As Athanasius suggested, mkdir was used to create the directory. Also, the file handle was closed.
    • And as LanX suggested, a three-argument open was used (and perhaps you meant something like open (my $fh,">D:/B/$output") ... ).

    Hope this helps!

      Hi Kenosis,

      Thanks a lot. Your code has worked nicely and solved my problem.

      With Regards

Re: How can one create a folder B in D drive and save results in a text file inside B using perl script?
by ww (Bishop) on Dec 10, 2012 at 11:46 UTC
    You've been here intermittently since 2009 but I have yet to see you ask a question that's not readily answered in one or more basic texts -- "Learning Perl" and the resources at learn.perl.org come to mind -- when those are seen as sources of more than mere code samples.

    Your remark about searching for code in the docs suggests that you misunderstand the code in docs as a mere source of code to cargo-cult rather than as samples illustrating the main text.

    And in this case, the logic of your code leads to the inference that you expected the directory "B" on drive "D" to auto-vivify. Windoze doesn't work that way, highlighting your failure to understand your OS.

    - - for lack of effort

      One of Perl's selling points is that people don't really need to learn it. Just post the problem on the web and some fanatic Perl programmer will do the work for you. Maybe it's the logical next step after open source: Ghost Source©.

      Unless this is really about inventing new emoticons with Perl => ... >D:// ...

      A poor, sad witto monk, hands clasped, begging for mercy? Not sure what kind of hat that would be though...

      --
      Tommy
      $ perl -MMIME::Base64 -e 'print decode_base64 "YWNlQHRvbW15YnV0bGVyLm1lCg=="'
      Clearly the OP is unaffected by such remarks and downvotes

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008035]
Approved by Athanasius
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-10-25 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls