How to call a .exe file in a ruby program


#1

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


#2

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.


#3

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


#4

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


#5

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.


#6

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.


#7

Give it in back quotes as shown below

output = my_app.exe


#8

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…