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

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>

Replies are listed 'Best First'.
Re: How can one create a text file in the subfolder of a folder?
by marto (Cardinal) on Apr 22, 2017 at 09:47 UTC

    Note the mkdir documentation states:

    "To recursively create a directory structure, look at the make_path function of the File::Path module."

    for example:

    #!/usr/bin/perl use strict; use warnings; use File::Path qw(make_path); my $x='x'; my $y='y'; my $dirname = "$x/$y"; make_path( $dirname , { chmod => 0777, }); 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";

    Results:

    D:\code>perl path.pl Program is over. D:\code>cd x/y D:\code\x\y>dir Volume in drive D is Data Volume Serial Number is 3241-B96B Directory of D:\code\x\y 22/04/2017 10:39 <DIR> . 22/04/2017 10:39 <DIR> .. 22/04/2017 10:39 13 z.txt 1 File(s) 13 bytes 2 Dir(s) 107,416,027,136 bytes free D:\code\x\y>cat z.txt It's ok. D:\code\x\y>

      Hi Marto,

      Thank you very much. Your code works well and has solved my problem.

      With kind regards,

      supriyoch_2008

Re: How can one create a text file in the subfolder of a folder?
by thanos1983 (Parson) on Apr 22, 2017 at 09:46 UTC

    Hello supriyoch_2008,

    Try something like that.

    #!/usr/bin/perl use strict; use File::Basename; use File::Path qw/make_path/; my $file = "/home/tinyos/Monks/SubFolder/foo.txt"; my $dir = dirname($file); make_path($dir); open my $fh, '>', $file or die "Ouch: $!\n"; print $fh $dir . "\n"; close $fh or die "Can not close file: $!\n"; __END__ $ perl dir.pl ~/Monks$ ll total 44 drwxr-xr-x 3 tinyos tinyos 4096 Apr 22 11:43 ./ drwxr-xr-x 31 tinyos tinyos 4096 Apr 22 10:34 ../ -rw-r--r-- 1 tinyos tinyos 299 Apr 22 11:43 dir.pl -rw-r--r-- 1 tinyos tinyos 214 Apr 22 11:39 dir.pl~ -rw-r--r-- 1 tinyos tinyos 2215 Apr 22 01:12 excel.pl -rw-r--r-- 1 tinyos tinyos 2182 Apr 20 00:53 excel.pl~ -rw-r--r-- 1 tinyos tinyos 5632 Apr 22 01:12 perl.xls -rw-r--r-- 1 tinyos tinyos 6656 Apr 19 20:43 Report.xls drwxr-xr-x 2 tinyos tinyos 4096 Apr 22 11:42 SubFolder/ ~/Monks$ cat SubFolder/foo.txt /home/tinyos/Monks/SubFolder

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Hi Thanos1983,

      Thanks. My problem has been solved.

      With regards,

      supriyoch_2008

Re: How can one create a text file in the subfolder of a folder?
by shmem (Chancellor) on Apr 22, 2017 at 09:56 UTC

    The problem is

    map {mkdir $_; chdir $_;} @folders;

    that you don't chdir back to where you came from.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Hi Shmem,

      Thank you for your suggestion.

      With kind regards,

      supriyoch_2008

        Thank you for your suggestion.

        You're welcome. But what I wrote wasn't a suggestion - it was telling you where the problem is.
        But since you thank me for a suggestion, here is one:

        use Cwd; my $cwd = getcwd(); ... map {mkdir $_; chdir $_;} @folders; chdir $cwd;
        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

        Or:

        map {mkdir $_; chdir $_;} @folders; my $output="z.txt"; open (my $fh,">",$output) or die "Cannot open file '$dirname/$output'. +\n";

        That, of course, assumes your code doesn't have other assumptions about the current folder.

        Much better to use File::Path, Path::Tiny or similar module.

Re: How can one create a text file in the subfolder of a folder?
by kevbot (Vicar) on Apr 22, 2017 at 16:08 UTC
    Here is a way to do it with Path::Tiny.
    #!/usr/bin/env perl use strict; use warnings; use Path::Tiny; my $x='x'; my $y='y'; my $dirpath = path($x, $y); $dirpath->mkpath; my $outpath = path( $dirpath, 'z.txt' ); my $fh = $outpath->openw_utf8; print $fh "\n It's ok.\n"; $fh->close; print "\n Program is over.\n"; exit;