实际上我正在使用以下代码在文件中查找路径:
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/