ruby copy folder with subfolders to a target location
Asked Answered
S

1

29

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
Soekarno answered 4/7, 2013 at 11:26 Comment(0)
I
43

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
Immediate answered 4/7, 2013 at 12:2 Comment(3)
Works for me, too.Passive
Threadnecromancer, @Soekarno please accept the answer if it worked for you.Risky
Although this works great, it's still better to wrap it around the begin-rescue block.Melisamelisande

© 2022 - 2024 — McMap. All rights reserved.