How to open file in its default application using Ruby scripts?
Let us say, I have folder with files .txt .doc .rb and I would like open them with Notepad, Word, and RubyMine respectively. I assume that all files have their default applications to open.
This should work (untested, as I'm not on a Windows machine now):
file_to_open = "c:\path\to\file.txt"
system %{cmd /c "start #{file_to_open}"}
For reference, this can also be done on OS X:
file_to_open = "/path/to/file.txt"
system %{open "#{file_to_open}"}
The best way to do this is to use the OS gem
require 'os'
OS.open_file_command
=> "start" # or open on mac, or xdg-open on linux (all designed to open a file)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With