# File.new and non-existent directories

**URL:** <https://www.ruby-forum.com/t/file-new-and-non-existent-directories/106736>\
**Category:** Ruby\
**Created:** [June 13, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/file-new-and-non-existent-directories/106736 "2007-06-13T18:00:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![studbucket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@studbucket](https://www.ruby-forum.com/u/studbucket)\
**Post date:** [June 13, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/file-new-and-non-existent-directories/106736/1 "2007-06-13T18:00:07Z")

</div>

Hi,

I may be wrong, but after some testing it appears to me that when I  
attempt to create a new file using File.new in a directory that does not  
exist, instead of creating the directory, an exception is thrown  
instead.

To give some background, I am archiving an email listserv so it can be  
searched. I’ve given the listserv administrator the ability to upload a  
word document which is parsed and converted into a text file, and stored  
both in a database and in a folder hierarchy on the filesystem. The  
folder hierarchy starts with folders of each year (2001, 2002, etc), and  
each year contains one folder per month (05.2004, 10.2004, etc). So  
when a listserv posting from May, 2007 is added, it would go to  
“somedir/2007/05.2007/posting\_name.txt”. I’ve had no problems when the  
folder already exists, but I would like it to be able to create the new  
directories needed when a posting from a new month or year is added, so  
we don’t have to continually add new folders to keep things running  
smoothly.

Can anyone direct me to a function, flag, or strategy to achieve what I  
am looking for?

Thanks,

JT

---

<div class="post-metadata">

**Author:** ![studbucket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@studbucket](https://www.ruby-forum.com/u/studbucket)\
**Post date:** [June 13, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/file-new-and-non-existent-directories/106736/2 "2007-06-13T18:09:42Z")

</div>

You are correct, File.new creates files, not directories.

Check out Dir and FileUtils: [RDoc Documentation](http://ruby-doc.org/core/)

FileUtils.mkdir “…” unless File.directory?(‘…’)

Jason

---

<div class="post-metadata">

**Author:** ![studbucket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@studbucket](https://www.ruby-forum.com/u/studbucket)\
**Post date:** [June 13, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/file-new-and-non-existent-directories/106736/3 "2007-06-13T18:08:41Z")

</div>

On 6/13/07, JT Kimbell [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I may be wrong, but after some testing it appears to me that when I  
> attempt to create a new file using File.new in a directory that does not  
> exist, instead of creating the directory, an exception is thrown  
> instead.

You could use Dir::mkdir or FileUtils#mkdir to create a directory  
and FileUtils#mkdir\_p if you want to create a directory and its  
parents directories (if they doesn’t exists).

---

<div class="post-metadata">

**Author:** ![studbucket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@studbucket](https://www.ruby-forum.com/u/studbucket)\
**Post date:** [June 13, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/file-new-and-non-existent-directories/106736/4 "2007-06-13T18:32:44Z")

</div>

On 6/13/07, Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You are correct, File.new creates files, not directories.
> 
> Check out Dir and FileUtils: [RDoc Documentation](http://ruby-doc.org/core/)
> 
> FileUtils.mkdir “…” unless File.directory?(‘…’)

And also (in the spirit of TMTOWTDI):

require ‘ftools’  
File.makedirs(“dir1/dir2/dir3”)

As covered before (  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/50237](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/50237) )

Cheers,  
Peter C.

> **[Ruby Inside: The Ruby Blog](http://www.rubyinside.com/)**
>
> The biggest Ruby and Rails blog since 2006 - tutorials, news, and lots more

---

<div class="post-metadata">

**Author:** ![studbucket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@studbucket](https://www.ruby-forum.com/u/studbucket)\
**Post date:** [June 13, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/file-new-and-non-existent-directories/106736/5 "2007-06-13T19:41:10Z")

</div>

Thanks everyone for your help!
