Hello -
I have the following code that runs a system command (In this example,
ipconfig), saves the output to a text file, then reads the text from the
text file back to the console.
Can you recommend a better, more succinct way to do the following.
Note: I do need to save the text to a text file (To have as a log file).
Thank you:
def test_save
File.open(‘C:/test.txt’, ‘w’) do |x|
x.write( ipconfig
)
end
File.open(‘C:/test.txt’, ‘r’).each_line do
|x| puts x
end
end
Igor I. wrote in post #1009383:
I have the following code that runs a system command (In this example,
ipconfig), saves the output to a text file, then reads the text from the
text file back to the console.
Can you recommend a better, more succinct way to do the following.
Note: I do need to save the text to a text file (To have as a log file).
Well, you could read it to a string, rather than reading it back from a
file:
res = ipconfig
puts res
File.open(“test.txt”,“w” { |x| x.write(res) }
Or you could write it to a file in the first place, and read that file:
system(“ipconfig >test.txt”)
res = File.read(“test.txt”)
Or you could write it to a file and to the screen in one go:
system(“ipconfig | tee test.txt”)
(That might only work on *nix though)
Also look at IO.popen.