# Simple FileUtils question

**URL:** <https://www.ruby-forum.com/t/simple-fileutils-question/63310>\
**Category:** Rails\
**Created:** [June 14, 2006, 3:59am UTC](https://www.ruby-forum.com/t/simple-fileutils-question/63310 "2006-06-14T03:59:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darkwax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Darkwax](https://www.ruby-forum.com/u/Darkwax)\
**Post date:** [June 14, 2006, 3:59am UTC](https://www.ruby-forum.com/t/simple-fileutils-question/63310/1 "2006-06-14T03:59:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Darkwax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Darkwax](https://www.ruby-forum.com/u/Darkwax)\
**Post date:** [June 14, 2006, 3:45pm UTC](https://www.ruby-forum.com/t/simple-fileutils-question/63310/2 "2006-06-14T15:45:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Darkwax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Darkwax](https://www.ruby-forum.com/u/Darkwax)\
**Post date:** [June 14, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/simple-fileutils-question/63310/3 "2006-06-14T19:55:16Z")

</div>

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
