Forum: Ruby Call Windows .exe with arguments

Posted by Charlie B. (charlie_b84)
on 2013-01-07 23:44
Trying to call a windows .EXE with additional arguments.  I found some
direction on calling a .EXE here, such as:

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

However what if I need to run "test.exe /argument /loadfile c:\test.txt
/path c:\windows /localos"

I can't find any syntax help with that.  Any suggestions?  I've done
trial and error with backticks and quotes but can't seem to nail it
down.
Posted by Damián M. González (igorjorobus)
on 2013-01-08 00:42
Well, I'm not an expert on this but look: depends of the situation.
What's that .exe? how it was generated? When you compile your proyect,
you can specifically pass to the compiler the arguments which you want
to be executed, and they will be fixeds, you can't change them later, 
but apparently you can append arguments later to the .exe generated.
For example, with Ocra compiler, for windows, you can do this:

"
Command Line Arguments
To pass command line argument to your script (both while building and
when run from the resulting executable), specify them after a “—”
marker. For example:

   ocra script.rb -- --some-options=value

This will pass “—some-options=value” to the script when build and when
running the executable. Any extra argument specified by the user when
invoking the executable will be appended after the compile-time
arguments.
"

 Meaby you can't do this with any .exe, meaby depends of the compiled 
which created the .exe. Tell me later if this works for you.
 Hope this helps and if anybody want to correct me if I'm wrong or add
something will be nice. Cheers.
Posted by Charlie B. (charlie_b84)
on 2013-01-08 05:59
The exe is not something I created.  In this case it is a windows native 
executable file (think notepad.exe or calc.exe).  I just need to call it 
from the windows system32 directory and pass arguments to it.  I just 
can't figure out how to do that.  Maybe set the arguments to a variable 
and pass the variable?  I'm pretty sure it's just a matter of getting 
the right combo of quotes or back ticks but I can't find the magic 
combination.
Posted by Damián M. González (igorjorobus)
on 2013-01-08 11:01
Yes, are you sure that the .exe accept arguments?
Posted by Charlie B. (charlie_b84)
on 2013-01-08 14:00
Yes.  I provided the arguments I need to pass above.  I can run that exe 
manually from the cli but I need to ruby to do it.
Posted by Charlie B. (charlie_b84)
on 2013-01-08 15:16
Maybe arguments isn't the best word.  Switches for the exe is maybe more 
descriptive.
Posted by Bartosz Dziewoński (matmarex)
on 2013-01-08 16:02
(Received via mailing list)
On Tue, 08 Jan 2013 11:01:36 +0100, Damián M. González 
<lists@ruby-forum.com> wrote:

> Yes, are you sure that the .exe accept arguments?

Of course they do.

There are two primary ways to call other programs or command from Ruby: 
either using the backtick syntax (which internally calls Kernel#`) or 
using Kernel#system. (There is also Kernel#spawn for heavy-duty work, 
when you need fine-grained control.)

These two differ in how they're called and what's the result. Backticks 
returns the output of the called shell command, and are processed simply 
in the same way the command would be processed in cmd.exe shell.

So to call program1.exe with "a" and "my dog" as arguments, returning 
its output:

   `program1.exe a "my dog"`

A Kernel#system call returns a truthy value if the call "succeded" (the 
program returned a 0 exit code), or a falsy one if it failed (syntax 
error or nonzero return value). It also accepts multiple arguments: if 
you only provie one, it acts in the same way as backticks (the string is 
parsed as a comment by the shell). If you provide multiple, arguments 
will be passed directly to the called program.


So to call program1.exe with "a" and "my dog" as arguments, returning 
whether the call worked correctly or not:

   system "program1.exe a \"my dog\""
   system "program1.exe", "a", "my dog"

I greatly advise you to use the second form, as it automatically escapes 
the arguments - this is especially important if their contents come from 
the user, as passing them straight to shell could be a security 
vulnerability.

There are more examples in the docs: 
http://www.ruby-doc.org/core-1.9.3/Kernel.html
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.