【问题标题】:ruby copy folder with subfolders to a target locationruby 将带有子文件夹的文件夹复制到目标位置
【发布时间】:2013-07-02 09:19:52
【问题描述】:

我正在尝试将一个文件夹及其所有子文件夹从源文件夹复制到目标文件夹。 下面的代码似乎没有这样做。我可以使用

仅复制文件而不是子文件夹
FileUtils.cp_r(Dir["/Volumes/TempData/Collects/Sasi/android/*.*"],"/Volumes/Data/Apps/android")

我错过了什么?

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

【问题讨论】:

    标签: ruby copy directory fileutils


    【解决方案1】:

    看起来 FileUtils.copy_entry 方法会为你复制一个目录树。 rubydoc 中有一些信息:http://www.ruby-doc.org/stdlib-2.0/libdoc/fileutils/rdoc/FileUtils.html#method-c-copy_entry

    有很多选项(例如是否保留文件所有权),但一些快速测试表明您可以像这样传递源目录和目标目录:

    FileUtils.copy_entry @source, @destination
    

    【讨论】:

    • 也适合我。
    • Threadnecromancer, @sasi 如果它对你有用,请接受答案。
    • 虽然这很好用,但最好将它包裹在 begin-rescue 块周围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    相关资源
    最近更新 更多