SlySherZ
SlySherZ

Reputation: 1661

How to launch file

I need a reliable way to launch a file through ruby. If the file is an mp3, it will play, if it is a txt notepad will open and so on. Basically I'm trying to use the "folder\folder\filename" behavior on the command line. The filename might contain spaces. I tried a couple of things already, like:

´"folder\\example.txt"´      //With backsticks instead

but

Exec format error - "folder\example.txt" (Errno::ENOEXEC)

and

filename = "folder\\example.txt"
proc = Process.spawn "\"#{filename}\""
Process.detach(proc)

and

system "\"folder\\example.txt\""

that does nothing.

How can I launch a file from ruby?

NEW INFO I tried to wrap the music name in " instead of the all name and it worked. But some folders have spaces, so this is not a solution. When start fails a new empty cmd opens for some reason.

Upvotes: 1

Views: 101

Answers (2)

Alex McMillan
Alex McMillan

Reputation: 1

You can use, as an example the calculator on Windows:

puts ( "#{%x{calc}}" )

Upvotes: 0

Stephan
Stephan

Reputation: 56155

the windows start command is bad designed. The first parameter is taken as the title of the new process, if enclosed in quotes. Best practice: always give it a first parameter in quotes (if needed or not). It can be empty. Example:

start "" "c:\my directory\file.txt"

or

start "title" "c:\my directory\file.txt" 

of course you won't see that title anywhere, as this will start notepad which does not support a "title".

Upvotes: 2

Related Questions