0

I'm writing a little Ruby script ( on Windows XP) that needs check ENV for a few things ( specifically if a few things are set and if they are if they are set to the proper values. If not and and if they are not present it needs to modify and ENV or overwrite value. For instance if ENV["CUSTOM_PATH"] doesn't contain C:\some_program\bin or it doesn't exist then it needs to be added and or modified and ENV saved.

Just to clarify - I want the change to be permanent and have system-wide effect, not just the current session. Basically I'm trying to create a shortcut for going to My COmputer -> Properties -> Advanced -> System Variables -> Changing a bunch of them by hand -> save. I need to do it 5-6 times a day while I'm developing and it's getting annoying pretty fast :-)

I got it all worked out except for last part - I can't figure out how to save ENV WITHOUT resorting to shell commands.

Is there anyway to do it directly from ruby, not shell?

Thank you!

WORKAROUND:

As specified in accepted answer - it's not possible to do. However my work around is dependent on combination of ruby and a command line utility called SETENV.EXE develped by Vincent Fatica. It's more than a decade old at this point but works fine in windows XP ( didn't test under Windows 7 yet). It works better than setx utility available from ms IMHO. At lest of deleting stuff.

Here is a short example of a method using it:

def switch_ruby_env
  if RUBY_VERSION.match("1.8.7").nil?  
    `setenv -m CUSTOM_PATH " "`
  else
    `setenv -m CUSTOM_PATH -delete`
  end
end 
konung
  • 6,908
  • 6
  • 54
  • 79

2 Answers2

1

Here's what I would do.

Call env_vals = batchfile_that_echos_ENV_values.bat

Then, read those env values, and if they are not correct then, batchfile_to_modify_env --key=value

Bob Chip
  • 182
  • 2
  • 6
1

Unfortunately you cannot have a process directly save an environment setting, however you can check out this:

Persisting an environment variable through Ruby

To see how to take advantage of saving the values in registry and making it eventually make it to the environment variables.

Community
  • 1
  • 1
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
  • Thank you Mike. That seems like a valid solution, but I can't be log off and on every 5 minutes when I'm testing - that's no faster than the manual process - no time savings :-) ( My machine takes about to 1.5 - 2 minutes to completely log on - a bunch of start up apps that need to run). I guess powershell or vbs is my only solution. Thanks for looking into it! – konung Mar 30 '11 at 18:46
  • You might want to consider running a VM for testing then. You can strip it down to the essential startup apps and nothing more, so its startup time is as fast as possible. I use them to build test configurations in Linux guests and it's very convenient. I like [VirtualBox](http://www.virtualbox.org/). – the Tin Man Mar 30 '11 at 18:53
  • Running a vm for this seems like an overkill. Plus I can't install virtualbox on every machine I'll need this script and a pieace of software on. Eventually it will need to be able to change environment while it's running and the swtich back, and it needs to be system wide - no way around it. I need to support some legacy software, that I have no control over. I just finished a little script using ruby & setx that does what I wanted. Thanks for suggestion thou. – konung Mar 30 '11 at 19:35