ruby copy folder with subfolders to a target location

sasi picture sasi · Jul 4, 2013 · Viewed 13.9k times · Source

I'm trying to copy a folder with all its subfolders from source to a destination folder. The below code doesn't seem to do it. I'm able to copy just the files instead of subfolders by using

FileUtils.cp_r(Dir["/Volumes/TempData/Collects/Sasi/android/*.*"],"/Volumes/Data/Apps/android")

What is it that I'm missing?

require 'fileutils'
puts "operating_system"
operating_system = gets.chomp

    if operating_system == "android" then
     FileUtils.cp_r(Dir["/Volumes/TempData/Collects/Sasi/android/**"],"/Volumes/Data/Apps/android")
     puts "done"
    elsif operating_system == "ios" then
     FileUtils.cp_r(Dir["Volumes/Data/Apps/iOS/CX5/**"],"/Volumes/TempData/Collects/For_CS")
     puts "done"
    else 
     puts "do nothing"
    end

Answer

Maikeru picture Maikeru · Jul 4, 2013

It looks like the FileUtils.copy_entry method will copy a directory tree for you. There is some information in the rubydoc : http://www.ruby-doc.org/stdlib-2.0/libdoc/fileutils/rdoc/FileUtils.html#method-c-copy_entry

There are lots of options (such as whether to preserve file ownership) but some quick testing shows that you can just pass the source and destination directories in like this:

FileUtils.copy_entry @source, @destination