Recursive mkdir

G

Greg Willits

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
 
G

Greg Willits

Greg said:
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
 
S

Seebs

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
 
B

Ben Bleything

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
 
L

Luis Lavena

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'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top