Copy a file, creating directories as necessary in Ruby

Typically it’s up to you to make sure that the target directory path exists, so I doubt if any built-in command does what you’re looking for.

But using FileUtils.mkdir_p(dir) could be very straightforward, especially by using File.dirname() to parse the path. You could even wrap it in a utility routine, e.g.:

require 'fileutils'

def copy_with_path(src, dst)
  FileUtils.mkdir_p(File.dirname(dst))
  FileUtils.cp(src, dst)
end

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)