Simple FileUtils question


#1

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.


#2

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?


#3

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