Yes, the core module File::Path - mkpath method can create subdirectories.
| [reply] [d/l] |
| [reply] [d/l] |
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. | [reply] [d/l] [select] |
Interestingly, Windows mkdir has the capability to create any intermediate directories on your behalf - meaning that simply
my $path = "c:\\a\\b\\c\\d"
system "mkdir $path";
should work as requested by the OP. Apparently, mkdir requires "command extensions" to be enabled to work in that way, whatever they are, but they're apparently (so say Microsoft) enabled by default for processes running under Windows XP anyway, which I presume extends to the Perl interpreter.
<sarcasm>Who needs cross-compatibility, anyway?</sarcasm> *grin*
| [reply] [d/l] [select] |
"should work", except that "c:\a\b\c\d" gets interpolated by perl as "c:" followed by ASCII "bell" ("\a"=0x07), backspace ("\b"=0x08), "ctl-\" ("\c\"=0x1c) and "d".
The File::Path module is the better solution, really, because it eliminates all the anxiety about backslashes and all the bizarre escaping needed to get them right, along with freeing you from OS-dependent peculiarities.
| [reply] |
Hi,
you can use mkdir with system(), but M$ mkdir does not understand forward slashes. It only takes Windows paths with baskslashes. In Perl again, you need to escape the backslash. Also, the -p switch is a Unix parameter:
$foldername = "Test1\\Test2"; #double backslash!
system "mkdir $foldername";
Regards,
svenXY | [reply] [d/l] |
thank you its helped me!!
| [reply] |