I am facing a certain problem while trying to open files from a directory ( the directory is provided on the command line )
My code is as follows:
require 'optparse'
OptionParser.new do |opts|
opts.banner = "Usage: rubyfile.rb [options]"
options[:c] = ""
opts.on( '-c', '--dir DILE', "Specify Directory with input files" ) do |e|
options[:c] = e
end
options[:o] = ""
opts.on( '-o', '--dir DILE', "Specify Output file" ) do |f|
options[:c] = f
end
end.parse!
out_file = File.open( options[:o], "a" )
dir_open = Dir.open( options[:c] )
Dir.foreach(dir_open) do | in_file |
next if File.directory? in_file
if( in_file.include? ".txt" )
input_file = File.open( in_file, "r" )
File.foreach(input_file).with_index do | line, line_s |
puts line
end
end
end
If I try to print out the file names using puts in_file, it works. But when I try to open the file and dump out its contents, it does not.
I know I am missing something here not able to pinpoint what it is though.