JohnDoe
JohnDoe

Reputation: 915

read path from a txt file and copy those file to a new directory with ruby

Actually I'm looking for paths within a file using this code:

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

That's so far OK, and I get this result in tmp.txt:

tmp.txt:

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

Now I would like to copy these c sources to an other directory "destination_dir". Something like this (as "pseudocode"):

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

Any idea how to do it with ruby?

Upvotes: 1

Views: 51

Answers (1)

Paritosh Piplewar
Paritosh Piplewar

Reputation: 8132

Not sure i followed the question but do you mean this

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

Upvotes: 3

Related Questions