Forum: Ruby how to call a .exe file in a ruby program

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
yukme (Guest)
on 2007-02-15 11:41
There is a executable file named "my_app.exe" and i want to run it in my
ruby program.
How should i do?
thanks
SonOfLilit (Guest)
on 2007-02-15 11:46
(Received via mailing list)
try a system() call.

Have a look in the docs at Kernel#system .

Also have a look at the adopt-a-newbie thread.

Aur S.
sur max (Guest)
on 2007-02-15 11:51
(Received via mailing list)
you can also do it as

exec("my_app.exe")

exec will throw an error if the exe will not be executed, whereas
system()will return true if exe runs properly and false otherwise
Harold H. (Guest)
on 2007-02-15 12:10
(Received via mailing list)
On 2/15/07, yukme <removed_email_address@domain.invalid> wrote:
> There is a executable file named "my_app.exe" and i want to run it in my
> ruby program.
> How should i do?
> thanks
>

Since you mention .exe I assume your on windows.

Have a look at IO.popen
http://www.ruby-doc.org/core/classes/IO.html#M002294

hth,
-Harold
SunRaySon (Guest)
on 2007-02-18 09:56
(Received via mailing list)
Give it in back quotes as shown below

output = `my_app.exe`
Sai S. (Guest)
on 2016-06-13 14:45
Hi All,

I am trying to run an exe from the ruby script, this exe is getting
called and everything looks good if there is only one Iteration. When i
tried to Iterate for fewer instances the control is getting ended at
first instance. Please have a look on the code mentioned below:


require 'csv'
require 'fileutils'
File.delete 'read1.txt'

dest_folder = File.join(Dir.pwd)

CSV.foreach("read.txt") do |row|        # read.txt contains the path
where xls and txt files are available.
  data_to =  "#{row}"
  concat = File.join(data_to)

  Source1 = File.join(concat, "*.txt")
  Source2 = File.join(concat, "*.xls")

  FileUtils.cp_r(Source1, dest_folder)
  FileUtils.cp_r(Source2, dest_folder)

  M = File.open("read1.txt", "a+")
  M.write"#{Source1}"
  M.write"\n"
  M.write"#{Source2}"
  M.write"\n"

  load "load.rb"
  sleep 3
  exec("My-Executable.exe")
  sleep 3
  puts "I am at end"
end

In the above source code i have a couple of issues:
1. In read.txt let us take an example 3 items are available, the loop
has to be run for 3 times but the control is getting exit in the first
loop itself, it was never executing the last two lines Sleep 3 and puts
statement.

2.  Source1 = File.join(concat, "*.txt")
I wanted to implement something like the above but i can't able to do
that instead i am sending "Source1 = File.join(concat, "file1.txt")"
everytime. I need a logic such that whatever the file name ends with
".txt" i wanted to copy from one folder to other folder.

I am using ruby 1.8.6.

Thanks in advance.
Ronald F. (Guest)
on 2016-06-13 15:04
Don't use exec!

Have a look at the definition of this function:

"Replaces the current process by running the given external command"

Hence, once you call exec, your calling process is gone.

In your case, using `system` is the best choice.
Sai S. (Guest)
on 2016-06-15 07:04
Ronald F. wrote in post #1183956:
> Don't use exec!
>
> Have a look at the definition of this function:
>
> "Replaces the current process by running the given external command"
>
> Hence, once you call exec, your calling process is gone.
>
> In your case, using `system` is the best choice.

Hi Ronald,

I have tried using system and is working fine. Thanks...
This topic is locked and can not be replied to.