Recursive mkdir

  • Follow


So, I was surprised to find out that Dir.mkdir will not create all
folders in a path when more than just the last level does not exist.
Example: Dir.mkdir('/f1/f2/f3') will not create /f3 when /f2 does not
exist. I expected it to make both /f2 and /f3 to get the job done. I
expected it because the language I used most previously did do that.

After some googling I'm not finding any elegant solutions.

Obviously I could split the path at / and iterate through each folder
name in sequence with an Exists? and mkdir follow up if needed.

Is that really the only option? To do this manually?

Just curious.

-- gw
-- 
Posted via http://www.ruby-forum.com/.

0
Reply lists5799 (97) 2/8/2010 9:11:52 PM

Greg Willits wrote:
> So, I was surprised to find out that Dir.mkdir will not create all
> folders in a path when more than just the last level does not exist.
> Example: Dir.mkdir('/f1/f2/f3') will not create /f3 when /f2 does not
> exist. I expected it to make both /f2 and /f3 to get the job done. I
> expected it because the language I used most previously did do that.
> 
> After some googling I'm not finding any elegant solutions.
> 
> Obviously I could split the path at / and iterate through each folder
> name in sequence with an Exists? and mkdir follow up if needed.
> 
> Is that really the only option? To do this manually?


ARGH. FileUtils.mkdir_p()

(never fails to find the answer right after posting)

-- gw
-- 
Posted via http://www.ruby-forum.com/.

0
Reply lists5799 (97) 2/8/2010 9:18:50 PM


On 2010-02-08, Greg Willits <lists@gregwillits.ws> wrote:
> So, I was surprised to find out that Dir.mkdir will not create all
> folders in a path when more than just the last level does not exist.
> Example: Dir.mkdir('/f1/f2/f3') will not create /f3 when /f2 does not
> exist. I expected it to make both /f2 and /f3 to get the job done. I
> expected it because the language I used most previously did do that.
>
> After some googling I'm not finding any elegant solutions.
>
> Obviously I could split the path at / and iterate through each folder
> name in sequence with an Exists? and mkdir follow up if needed.
>
> Is that really the only option? To do this manually?

%x{mkdir -p "#{dir}"}
?

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
0
Reply usenet-nospam (2199) 2/8/2010 9:22:23 PM

On Mon, Feb 8, 2010 at 1:11 PM, Greg Willits <lists@gregwillits.ws> wrote:
> So, I was surprised to find out that Dir.mkdir will not create all
> folders in a path when more than just the last level does not exist.
> Example: Dir.mkdir('/f1/f2/f3') will not create /f3 when /f2 does not
> exist. I expected it to make both /f2 and /f3 to get the job done. I
> expected it because the language I used most previously did do that.

Dir.mkdir emulates the unix mkdir command, which behaves this way.  It
shouldn't be surprising.

> After some googling I'm not finding any elegant solutions.

Did you try ri?

> Obviously I could split the path at / and iterate through each folder
> name in sequence with an Exists? and mkdir follow up if needed.
>
> Is that really the only option? To do this manually?

No.  Look into FileUtils, specifically FileUtils.mkdir_p

Ben

0
Reply ben9709 (266) 2/8/2010 9:26:36 PM


On Feb 8, 4:18=A0pm, Greg Willits <li...@gregwillits.ws> wrote:

> ARGH. FileUtils.mkdir_p()

Yea, but set $VERBOSE =3D true and watch all the pretty warnings.
Annoying.

0
Reply transfire (2969) 2/8/2010 9:38:53 PM

On Feb 8, 10:11=A0pm, Greg Willits <li...@gregwillits.ws> wrote:
> So, I was surprised to find out that Dir.mkdir will not create all
> folders in a path when more than just the last level does not exist.
> Example: Dir.mkdir('/f1/f2/f3') will not create /f3 when /f2 does not
> exist. I expected it to make both /f2 and /f3 to get the job done. I
> expected it because the language I used most previously did do that.
>
> After some googling I'm not finding any elegant solutions.
>

require 'fileutils'

FileUtils.mkdir_p 'my/path/to/something'

--
Luis Lavena
0
Reply luislavena (644) 2/9/2010 1:36:24 PM

5 Replies
36 Views

(page loaded in 0.451 seconds)

Similiar Articles:







7/15/2012 2:26:33 PM


Reply: