Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

mkdir problem on windows

by lamp (Chaplain)
on Sep 19, 2005 at 12:28 UTC ( [id://493136]=perlquestion: print w/replies, xml ) Need Help??

lamp has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

Is there a way in perl to provide a file-path in the 'mkdir' command. I want to accept a path the
user wants to create folders, such as 'Test1/Test2', where Test1 does not already exists. The following code fails creating
folders Test1 and Test2. It's working with only one folder. The perl compiler which I am using is Active(5.8.4) on Windows XP.

$foldername = "Test1/Test2"; system "mkdir -p $foldername";


Thank you in advance

Replies are listed 'Best First'.
Re:mkdir problem on windows
by sh1tn (Priest) on Sep 19, 2005 at 12:32 UTC
    Yes, the core module File::Path - mkpath method can create subdirectories.


Re: mkdir problem on windows
by prasadbabu (Prior) on Sep 19, 2005 at 12:34 UTC

    lamp, you can make use of File::Path

    mkpath (['test1/test2'],1,0711);

    Prasad

Re: mkdir problem on windows
by mulander (Monk) on Sep 19, 2005 at 12:59 UTC
    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.
      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.

Re: mkdir problem on windows
by svenXY (Deacon) on Sep 19, 2005 at 12:37 UTC
    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
      thank you its helped me!!

Log In?
Username:
Password:

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

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

    No recent polls found