setting environment variables using windows batch file

A

Afaque Ahmad

--0-1973032334-1117109779=:52407
Content-Type: text/plain; charset=us-ascii

Dear Ruby Members,

I am having problems setting and reading enviroment variables from a windows batch file.

This is what I am doing, I have some batch file xyz.bat where I am setting some enviroment
variable

SET ENV_VAR1 = "something"
SET ENV_VAR2 = "foo"

I wish to run the batch file from ruby script , wherein which I can use these environment variables within the ruby file itself. I use the following command to run the batch file

system("xyz.bat")

But even after doing this , the environment variables ENV_VAR1 and ENV_VAR2 don't get set.
I even tried using
exec("xyz.bat")
system("cmd /k xyz.bat")

I would really appreciate it if anybody could tell me what I am doing wrong here.

Regards,
Afaque
 
A

Austin Ziegler

I am having problems setting and reading enviroment variables from
a windows batch file.

Simply put, you can't do this. #exec and #system start new copies of
the command interpreter (cmd.exe) and the environment variables are
set in that environment, which is subordinate to the Ruby
interpreter process.

You'd be better off parsing the BAT file for SET commands. And using ENV.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
B

Brian Schröder

Dear Ruby Members,
=20
I am having problems setting and reading enviroment variables from a win= dows batch file.
=20
This is what I am doing, I have some batch file xyz.bat where I am settin= g some enviroment
variable
=20
SET ENV_VAR1 =3D "something"
SET ENV_VAR2 =3D "foo"
=20
I wish to run the batch file from ruby script , wherein which I can use t=
hese environment variables within the ruby file itself. I use the following=
command to run the batch file
=20
system("xyz.bat")
=20
But even after doing this , the environment variables ENV_VAR1 and ENV_VA= R2 don't get set.
I even tried using
exec("xyz.bat")
system("cmd /k xyz.bat")
=20
I would really appreciate it if anybody could tell me what I am doing wro= ng here.
=20

Hello Afaque,

it should be possible if you can invert your problem. If you start the
ruby interpreter from the batch file, you have the variables set.

--8<---test.bat---8<---
SET FOO =3D "foo"
ruby test.rb
--8<-----------------8<--


--8<---test.rb---8<---
puts ENV['FOO']
--8<-----------------8<--

If you want to use the batch file as some kind of configuration file,
maybe you could revert to a real configuration file. E.g. in yaml.

best regards,

Brian Schr=F6der

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top