7

I have a file located in the directory "C:\Documents and Settings\test.exe" but when I write the command `C:\Documents and Settings\test.exe in single qoutes(which I am not able to display in this box), used for executing the commands in Ruby, I am not able to do so and the error that I recieve is No file or Directory found. I have tried replacing "\" with "//" and "\" but nothing seems to work. I have also used system, IO.popen and exec commands but all efforts are in vain. Also exec commands makes the program to make an exit which I don't want to happen.

Thanks in advance.

Arup Rakshit
119k30 gold badges269 silver badges328 bronze badges
asked Apr 3, 2011 at 18:16
1
  • I'm assuming you bean "backtick" instead of "single quotes", but I'm not going to edit your question unless you confirm that. Commented Aug 25, 2011 at 12:30

2 Answers 2

8

The backtick environment is like double-quotes, so backslash are used for escaping. Further, Ruby will interpret the spaces as separating command-line arguments, so you need to quote the entire thing:

`"C:\\Documents and Settings\\test.exe"`

Another option is to use system and force a second argument. If system gets more than one argument, it treats the first argument as the path to the command to execute and you don't need to quote the command:

system('C:\Documents and Settings\test.exe','')

Note the use of single quotes, so we don't have escape the backslashes.

Of course, this won't get you the standard out/error, so if you are on Ruby 1.9.2, you can use the awesomely handy Open3 library, which works like system, but gives you more information about the process you just ran:

require 'open3'
stdout,stderr,status = Open3.capture3('C:\Documents and Settings\test.exe','')
puts stdout # => string containing standard output of your command
puts stderr # => string containing standard ERROR of your command
if status.success?
 puts "It worked!"
else
 puts "OH NOES! Got exit code #{status.exitstatus}"
end
David Sulc
26k3 gold badges54 silver badges54 bronze badges
answered Aug 25, 2011 at 12:29

1 Comment

undefined method `capture3' for Open3:Module (NoMethodError) with ruby 1.8.7
6
`"C:\Documents and Settings\test.exe"`

or

`exec "C:\Documents and Settings\test.exe"`

or whatever in qoutes

answered Apr 3, 2011 at 18:24

2 Comments

main idea is to wrap the path into quotes. I am not familiar with Windows so there can be any other restrictions.
Don't know if the file I am working with has some constraints to work in a Ruby program or the way I am specifying the path of the file is incorrect but the thing is not working for me... But the method that you have told is correct!!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.