Forum: Ruby Recursive mkdir

Posted by Greg Willits (-gw-)
on 2010-02-08 22:11
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 by Greg Willits (-gw-)
on 2010-02-08 22:18
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 by Ben Bleything (Guest)
on 2010-02-08 22:29
(Received via mailing list)
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
Posted by Seebs (Guest)
on 2010-02-08 22:31
(Received via mailing list)
On 2010-02-08, Greg Willits <lists@gregwillits.ws> wrote:
>
> Is that really the only option? To do this manually?

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

-s
Posted by Thomas Sawyer (7rans)
on 2010-02-08 22:45
(Received via mailing list)
On Feb 8, 4:18 pm, Greg Willits <li...@gregwillits.ws> wrote:

> ARGH. FileUtils.mkdir_p()

Yea, but set $VERBOSE = true and watch all the pretty warnings.
Annoying.
Posted by Luis Lavena (luislavena)
on 2010-02-09 14:41
(Received via mailing list)
On Feb 8, 10:11 pm, 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'
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.