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)
Philidor
  • 40,933
  • 9
  • 84
  • 94

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
Philidor
  • 40,933
  • 9
  • 84
  • 94
  • Thanks! Is there a way I can write the output to a file, instead of the console? –  Oct 27 '17 at 13:45
  • https://stackoverflow.com/questions/6674327/redirect-all-output-to-file – Philidor Oct 27 '17 at 13:49
  • Is there a way to turn the following into a method where the values are pulled, and written to a new txt file? –  Oct 27 '17 at 17:17