I would like to create a directory tree up to a certain "depth". This directory tree has sub directories, and those sub directories have even more subdirectories, all up to a certain depth.

I've spent half of the day trying to do this recursively, but for some reason, only the first directory of each level has subdirectories.

FYI, here is a stripped down version of the code I have:
#!/usr/bin/perl -w use Cwd; sub MakeDirs($$$) { #my $current_dir = getcwd(); my($base, $num_dirs, $depth) = @_; #if depth = 0, no more subdirectories need to be created if($depth == 0) { return 0; } #change the directory chdir $base; #Create directories on the current level my $dir_name = "a"; for($x = 0; $x < $num_dirs; $x++) { mkdir $dir_name; $dir_name++; } #recurse over subdirectories $dir_name = "a"; for($x = 0; $x < $num_dirs; $x++) { MakeDirs($dir_name, $num_dirs, $depth-1); $dir_name++; } return 0; } MakeDirs("/hpq/", 2, 3);
I would use File::Recurse, but the files/directories haven't been created yet. Thanks for any suggestions. This script is driving me crazy!

Originally posted as a Categorized Question.


In reply to How do I recursively create a directory tree/filesystem? by fensterblick

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.