ruby - 从 txt 文件中读取路径并使用 ruby​​ 将这些文件复制到新目录

标签 ruby

实际上我正在使用以下代码在文件中查找路径:

files_to_read = Dir['This/is/a_path/.sln']
files_to_read.each do |file_name|

  array = []

  # regex for the line where the source path is stored
  regex = /(\.\.\\[^"]+)/
  File.open(file_name) do |f|
    f.each_line do |line|
      regex.match(line) do |matches|
        array << matches[1]
        File.open("This/is/a_path/tmp.txt", 'w+') {|f| f.write array.join("\n")}
         end
      end
    end
  end
end

到目前为止一切正常,我在 tmp.txt 中得到了这个结果:

tmp.txt:

..\..\..\Project\Source\sourceA.c
..\..\..\Project\Source\ModuleB\sourceB.c

现在我想将这些 c 源代码复制到另一个目录“destination_dir”。像这样的东西(作为“伪代码”):

File.open("This/is/a_path/tmp.txt", 'r') do |f|
   f.each_line FileUtils.cp(<the_c_sources>, "destination_dir")

知道如何用 ruby​​ 做到这一点吗?

最佳答案

不确定我是否听懂了问题,但你是这个意思吗

tmp_file = File.read "This/is/a_path/tmp.txt"
tmp_file.lines.each do |file_name|
  FileUtils.cp(file_name, "destination_dir")
end

关于ruby - 从 txt 文件中读取路径并使用 ruby​​ 将这些文件复制到新目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34062611/

相关文章:

ruby - 为什么 random 在 Ruby 中这样工作?

ruby - Ruby 中多边形的点交点

ruby-on-rails - 如何将数字数组转换为范围

ruby-on-rails - Ruby:多行条件

ruby - 使用 Ruby 上传大文件

ruby-on-rails - 带条件求和的 PostgreSQL 语法,使用 Rails

ruby - 如何防止将位置参数扩展为关键字参数?

javascript - 保持 API 调用私有(private)/ protected

ruby-on-rails - 回形针,CKEditor validates_attachment_content_type 错误

ruby - 使用 RVM 的 Hadoop Streaming 找不到 Gem