Forum: Ruby File.new and non-existent directories

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
236e5f67c35a7db58901b2ac90f49b4f?d=identicon&s=25 JT Kimbell (studbucket)
on 2007-06-13 18:00
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
7e593ac63e5f25649b701dc25f69d1b7?d=identicon&s=25 Luis Parravicini (Guest)
on 2007-06-13 18:08
(Received via mailing list)
On 6/13/07, JT Kimbell <jtkimbell@yahoo.com> 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).
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-06-13 18:09
(Received via mailing list)
You are correct, File.new creates files, not directories.

Check out Dir and FileUtils: http://ruby-doc.org/core/

FileUtils.mkdir "..." unless File.directory?('...')

Jason
509e15b158eaac5ab3a5332a3f89d4a7?d=identicon&s=25 Peter Cooper (Guest)
on 2007-06-13 18:32
(Received via mailing list)
On 6/13/07, Jason Roelofs <jameskilton@gmail.com> wrote:
> You are correct, File.new creates files, not directories.
>
> Check out Dir and FileUtils: 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/... )

Cheers,
Peter Cooper
http://www.rubyinside.com/
236e5f67c35a7db58901b2ac90f49b4f?d=identicon&s=25 JT Kimbell (studbucket)
on 2007-06-13 19:41
Thanks everyone for your help!
This topic is locked and can not be replied to.