Need tutoring on using a path environment variable

I don’t know what to do. I have an environment variable, PW_PATH,
that contains the folder names where applications can be executed
from. I want to start a particular application that is in one of
those folders. I currently use the following:

  system("presort /a /nos /watch #{template[0]}")  # submit the

presort to run

This works when the contents of PW_PATH are added to the PATH
variable. But I would like to avoid that if I can. I guess my
questions are: How do I first acquire the contents of PW_PATH? How
do employ the contents as a temporary addition to PATH so that system
can submit the application and it will be found and executed?

Suggestions? Comments? Observations? All are appreciated.

dvn

dkmd_nielsen wrote:

I don’t know what to do. I have an environment variable, PW_PATH,
that contains the folder names where applications can be executed
from. I want to start a particular application that is in one of
those folders. I currently use the following:

  system("presort /a /nos /watch #{template[0]}")  # submit the

presort to run

This works when the contents of PW_PATH are added to the PATH
variable. But I would like to avoid that if I can. I guess my
questions are: How do I first acquire the contents of PW_PATH? How
do employ the contents as a temporary addition to PATH so that system
can submit the application and it will be found and executed?

Suggestions? Comments? Observations? All are appreciated.

dvn

ENV contains all environment variables; ENV[‘PW_PATH’] is the content of
PW_PATH. So this might be sufficient:

system("#{ENV[‘PW_PATH’]/presort /a /nos /watch #{template[0]}")

hth,

Siep

On Nov 12, 2:50 pm, dkmd_nielsen [email protected] wrote:

questions are: How do I first acquire the contents of PW_PATH? How
do employ the contents as a temporary addition to PATH so that system
can submit the application and it will be found and executed?

Suggestions? Comments? Observations? All are appreciated.

dvn

First question answered: pw = ENV[‘pw_path’] I got that. But I
don’t know how to employ the contents such that when the application
is submitted via system that the o/s actually knows where to go
looking.

On Nov 12, 3:06 pm, Siep K. [email protected] wrote:

variable. But I would like to avoid that if I can. I guess my

system(“#{ENV[‘PW_PATH’]/presort /a /nos /watch #{template[0]}”)

hth,

Siep


Posted viahttp://www.ruby-forum.com/.

Thanks, Siep. I will give this a shot. Not sure if linux and windows/
dos will treat it the same, but I guess that’s my job to figure out.

On Nov 12, 2008, at 6:22 PM, dkmd_nielsen wrote:

the search path of PATH.

Is that better?

you can always store the path before and reset it after the system call:

old_path = ENV[‘path’]
ENV[‘path’] << ENV[‘PW_PATH’]
system(…)
ENV[‘path’] = old_path

dvn

regards,

Let’s provide more information. The contents of pw_path include this:

C:\PW\firstprep;C:\PW\rapid\docs;C:\PW\la;C:\PW\rapid;c:\pw\pst;

And the application I want to execute is presort.exe, which is located
in c:\pw\pst;

I would like to be able to tell the O/S you have to look in the above
folders to find presort.exe, just as you would using PATH. In a
sense, I would like to temporarily merge the contents of pw_path into
the search path of PATH.

Is that better?
dvn

On Nov 12, 2008, at 10:30 PM, Rob B. wrote:

Of course, doing that kind of thing in Ruby should look like:

begin
old_path = ENV[‘path’]
ENV[‘path’] << ENV[‘PW_PATH’]
system(…)
ensure
ENV[‘path’] = old_path
end

totally right… although, I don’t think that system will raise an
exception. Maybe if PATH is blank (i.e. nil), concatenating the other
environmental variable will raise an exception.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

regards,

On Nov 12, 2008, at 7:20 PM, Rolando A. wrote:

I would like to be able to tell the O/S you have to look in the above
ENV[‘path’] << ENV[‘PW_PATH’]
system(…)
ENV[‘path’] = old_path

dvn

regards,

Rolando A. M.

Of course, doing that kind of thing in Ruby should look like:

begin
old_path = ENV[‘path’]
ENV[‘path’] << ENV[‘PW_PATH’]
system(…)
ensure
ENV[‘path’] = old_path
end

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Nov 13, 2008, at 11:42 AM, Yossef M. wrote:

Maybe you mean ENV[‘path’] += ENV[‘PW_PATH’], or maybe you mean
old_path = ENV[‘path’].dup

oops… you’re right (I always forget about this! I even wrote a
question about this for the ruby-basic java black belt[1]
certification test :-P)

Second, paths have separators, so simply sticking two different path
strings together is going to make something strange at the join.

yes sir, I skip those on purpose… :slight_smile: (it was too late and it was
the last email I wrote before going to bed. I think that the OP should
know that PATH is a list of directories that are separated by
something, or at least he has the notion, so I only provided an idea
on how to solve his problem, not a complete solution, which, IMHO is
the best thing, because when you provide a complete solution, most
people stick to that and don’t think too much around it).
Anyway, you should add the path like this:

ENV[‘PATH’] += “;#{ENV[‘PW_PATH’]}” # just in case ENV[‘PW_PATH’] is nil

even if PATH has a “;” at the end, it does no harm to have another one.

Third, I think you mean ENV[‘PATH’], not ENV[‘path’].

That’s also correct.

HTH HAND

-yossef

[1]
http://www.javablackbelt.com/QuestionnaireDefDisplay.wwa?questPublicId=01548

regards,

On Nov 12, 6:20 pm, Rolando A. [email protected] wrote:

you can always store the path before and reset it after the system call:

old_path = ENV[‘path’]
ENV[‘path’] << ENV[‘PW_PATH’]
system(…)
ENV[‘path’] = old_path

First,

str = ‘blah’
=> “blah”
old_str = str
=> “blah”
str << ‘test’
=> “blahtest”
str
=> “blahtest”
old_str
=> “blahtest”

Maybe you mean ENV[‘path’] += ENV[‘PW_PATH’], or maybe you mean
old_path = ENV[‘path’].dup

Second, paths have separators, so simply sticking two different path
strings together is going to make something strange at the join.

Third, I think you mean ENV[‘PATH’], not ENV[‘path’].

HTH HAND