Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby copy folder with subfolders to a target location

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 
like image 585
sasi Avatar asked Jul 04 '13 11:07

sasi


People also ask

How do I copy a folder and subfolders?

Type "xcopy", "source", "destination" /t /e in the Command Prompt window. Instead of “ source ,” type the path of the folder hierarchy you want to copy. Instead of “ destination ,” enter the path where you want to store the copied folder structure. Press “Enter” on your keyboard.

How do I copy a folder from one directory to another in VB net?

Use the CopyDirectory method to copy a directory to another directory. This method copies the contents of the directory as well as the directory itself. If the target directory does not exist, it will be created.

How do you move a file in Ruby?

You don't need fileutils or a systemcall, just rename the file to the new location. File rename won't work across partitions, and throws the error "Invalid cross-device link". FileUtils is a better choice in those cases, but for a simple move in the same partition, rename works.


1 Answers

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 
like image 108
Maikeru Avatar answered Sep 29 '22 18:09

Maikeru