Hi Perlmonks,

My interest is to create a text file z.txt inside a subfolder y of the main folder x. I want to write in the text file using a file handle like $fh. I searched for perl examples online but did not find one which can solve my problem. I have written a script x4.pl which creates folders x and y and not the file. The result in cmd shows "Cannot open file 'x/y/z.txt'". I welcome suggestions from the perlmonks to sort out this problem.

Here goes my script x4.pl:

#!/usr/bin/perl use strict; use warnings; my $x='x'; my $y='y'; my $dirname = "$x/$y"; my @folders = split /\/|\\/, $dirname; map {mkdir $_; chdir $_;} @folders; # Print output to a Text File: my $output="$dirname/z.txt"; open (my $fh,">",$output) or die "Cannot open file '$output'.\n"; print $fh "\n It's ok.\n"; close $output; print "\n Program is over.\n"; exit;

Here goes the result of cmd:

Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users>cd d* C:\Users\Desktop>x4.pl Cannot open file 'x/y/z.txt'. C:\Users\Desktop>

In reply to How can one create a text file in the subfolder of a folder? by supriyoch_2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.