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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With