Forum: Ruby on Rails Simple FileUtils question

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.
Darkwax (Guest)
on 2006-06-14 03:59
Hi, I have a quick question. I am trying to create folders dinamically
using FileUtils.mkdir, the problem is that if the folder already exist
it gives me an error. Does anyone know if there are any options to mkdir
that would let me overwrite the already existing directory? I just need
to create a folder with the same name everytime. Any other solution you
might suggest? Thanks.
Chris Hall (Guest)
on 2006-06-14 15:45
(Received via mailing list)
if the directory already exists, then why the need to overwrite it?

here's a little module i use

lib/myfileutils.rb

module MyFileUtils
  # method: MyFileUtils.mkdir
  # params: directory path as string, ex: /some/path
  # returns: true - created directory, false = directory exists, nil -
could
not create directory
  def self.mkdir(dir)
    begin
      return false if File.directory?(dir) # directory already exists
      return true if Dir.mkdir(dir) # successfully created directory
    rescue SystemCallError => e
      return nil # can't create the directory (most likely permissions
problem)
    end
  end
end


require 'myfileutils'

do_something unless MyFileUtils.mkdir("/some/path").nil?
Chris Hulan (Guest)
on 2006-06-14 19:55
Darkwax wrote:
> Hi, I have a quick question. I am trying to create folders dinamically
> using FileUtils.mkdir, the problem is that if the folder already exist
> it gives me an error. Does anyone know if there are any options to mkdir
> that would let me overwrite the already existing directory? I just need
> to create a folder with the same name everytime. Any other solution you
> might suggest? Thanks.

you could try:

  FileUtils.remove_dir(path, true) #true will force ignoring any errors
  FileUtils.mkdir(path)

cheers
Chris
This topic is locked and can not be replied to.