Don't ask to ask, just ask | |
PerlMonks |
Re: How do I recursively create a directory tree/filesystem?by Ionitor (Scribe) |
on Jul 22, 2002 at 00:30 UTC ( [id://183902]=note: print w/replies, xml ) | Need Help?? |
The major problem here is that you're using chdir and not returning to the right directory afterwards. Since you're changing the directory of the program, the recursion doesn't work. The fastest way to fix the problem is to add chdir '..'; before return 0;. There's a couple of other changes you might want to make described below, one of which is to avoid chdir entirely.
First, I added use strict and made a couple changes to make that work. I also removed the pointless return 0;. Next, I removed the chdir and changed the recursion line to: This avoids changing the directory and leaves you where you were if the program quits midway through for some reason. Finally, I removed the mkdir loop, and added mkdir $base; near the top of the program. This makes every recursion make one directory, which is how I like my recursion to work. It also has the added advantage of creating the initial directory if it doesn't exist. My finished code follows:
In Section
Seekers of Perl Wisdom
|
|