Hello fellow Monk.
First of all do not create directories using the system command, instead use perl build in mkdir(); function.
I understand that you want to create directories recursiv, in example if provided Test1/Test2 you want the Test1 directory to be created first and then Test2 created inside of Test1, did I get it right?
If so here is a simple script that I wrote as an example for your case, it does the job but probably can be better written.
#!/usr/bin/perl
use warnings;
use strict;
my $foldername = "Test1/Test2";
my @folders = split /\//,$foldername;
for (0 .. $#folders) {
mkdir -p $folders[$_];
chdir $folders[$_];
}
A simple explanation to the code above:
We use split to get a list of folder names ( if you want the user to supply the folder names be sure to check if he used a slash ( / ) or a backslash ( \ ) you will have to modify the regular expression in split for this ).
The for loop iterates over the entire list, as you can see inside every iteration we create a directory using mkdir function of perl, and chdir function ( also build in ) to descend into the newly created directory. The loop stops when it creates the last directory from the list.
I do not know why you tried to use the -p switch on the call to windows mkdir, as far as I remember, windows does not support switches on its command line, so you would just create a directory '-p'. I tested my script on windows xp, and it work the way it is supposed to :) hope I this reply will help you at least a bit, or give a new view on the subject.
PS.
I came up with another way to get the job done and here it is:
#!/usr/bin/perl
use warnings;
use strict;
my $foldername = "Test1/Test2";
my @folders = split /\/|\\/, $foldername;
map { mkdir $_; chdir $_; } @folders;
I think that using map will work faster here, and also i modified the regular expression in split so it handles both directories delimited with / and \, it works fine on my windows xp.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.