I have a script where I iterate through some directories and create files there and put some strings inside the created files. Within the directories where I create my files, there exist some other files which I would like to open and search for some specific strings to copy them into my created files. This existing files have the ".sln" extension .
Is there an easy way to tell Ruby just in that moment when I create my file, to open the existing .sln file and to parse it. I imagine something like:
File.open('*.sln', 'r')
I have tried something like this:
sln_files = Dir.glob('*.sln', 'r')
File.open(sln_files)
and also this method:
Dir["/path/to/search/**/*.sln"]
but it gives me the full path to the file. I need only the file names
Any idea? I would like to have the possibility to use wildcards within the File.open
method
So I found the solution at an already existing topic (Get names of all files from a folder with Ruby). It was:
files = Dir["/work/myfolder/**/*.txt"]
files.each do |file_name|
if !File.directory? file_name
puts file_name
File.open(file_name) do |f|
f.each_line do |line|
if line =~ /banco1/
puts "Found: #{line}"
end
end
end
end
end