0

I have this script where I can pull a string based on the line and position of it. I got it working for one file, but how do I get it to work for all files in a directory.

Here is the code:

def pull_value(files, line_num, gbegin, gend)
   File.readlines(files)[line_num][gbegin..gend]
end

puts ("some/directory/file.txt", 10, 1, 7)
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103

1 Answers1

1

You can use Dir.glob("*.txt").

There's "*.txt" argument mean all files in the current directory with txt extension:

def pull_value(files, line_num, gbegin, gend)
   File.readlines(files)[line_num][gbegin..gend]
end

Dir.glob("*.txt").each do |f|
  pull_value(f, 10, 1, 7)
end
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103