Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: mkdir problem on windows

by mulander (Monk)
on Sep 19, 2005 at 12:59 UTC ( [id://493143]=note: print w/replies, xml ) Need Help??


in reply to mkdir problem on windows

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.

Replies are listed 'Best First'.
Re^2: mkdir problem on windows
by Tanalis (Curate) on Sep 19, 2005 at 13:51 UTC
    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*

      "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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://493143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found