# How to call a .exe file in a ruby program

**URL:** <https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655>\
**Category:** Ruby\
**Created:** [February 15, 2007, 10:41am UTC](https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655 "2007-02-15T10:41:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yukme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/da6949/32.png) [@yukme](https://www.ruby-forum.com/u/yukme)\
**Post date:** [February 15, 2007, 10:41am UTC](https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655/1 "2007-02-15T10:41:46Z")

</div>

There is a executable file named “my\_app.exe” and i want to run it in my  
ruby program.  
How should i do?  
thanks

---

<div class="post-metadata">

**Author:** ![yukme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/da6949/32.png) [@yukme](https://www.ruby-forum.com/u/yukme)\
**Post date:** [February 15, 2007, 10:46am UTC](https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655/2 "2007-02-15T10:46:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yukme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/da6949/32.png) [@yukme](https://www.ruby-forum.com/u/yukme)\
**Post date:** [February 15, 2007, 10:51am UTC](https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655/3 "2007-02-15T10:51:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yukme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/da6949/32.png) [@yukme](https://www.ruby-forum.com/u/yukme)\
**Post date:** [February 15, 2007, 11:10am UTC](https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655/4 "2007-02-15T11:10:46Z")

</div>

On 2/15/07, yukme [removed\_email\_address@domain.invalid](mailto: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](http://www.ruby-doc.org/core/classes/IO.html#M002294)

hth,  
-Harold

---

<div class="post-metadata">

**Author:** ![yukme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/da6949/32.png) [@yukme](https://www.ruby-forum.com/u/yukme)\
**Post date:** [June 13, 2016, 12:45pm UTC](https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655/5 "2016-06-13T12:45:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yukme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/da6949/32.png) [@yukme](https://www.ruby-forum.com/u/yukme)\
**Post date:** [June 13, 2016, 1:04pm UTC](https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655/6 "2016-06-13T13:04:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yukme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/da6949/32.png) [@yukme](https://www.ruby-forum.com/u/yukme)\
**Post date:** [February 18, 2007, 8:56am UTC](https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655/7 "2007-02-18T08:56:04Z")

</div>

Give it in back quotes as shown below

output = `my_app.exe`

---

<div class="post-metadata">

**Author:** ![yukme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/da6949/32.png) [@yukme](https://www.ruby-forum.com/u/yukme)\
**Post date:** [June 15, 2016, 5:04am UTC](https://www.ruby-forum.com/t/how-to-call-a-exe-file-in-a-ruby-program/85655/8 "2016-06-15T05:04:38Z")

</div>

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…
