NaSe77 has asked for the wisdom of the Perl Monks concerning the following question:

He there!

I am writing an installation script for some other scripts I have written to work under win2000. So far not a great problem but in order to make it easier for the user i would like to change/add some system variables too (like %Path% and %HTTP_proxy%) and this should be permanent and systemwide.

Any ideas or suggestions ? Thanx

----
NaSe
:x

  • Comment on Setting and changing system variables under win2000

Replies are listed 'Best First'.
Re: Setting and changing system variables under win2000
by strat (Canon) on Aug 13, 2002 at 11:16 UTC
    perhaps the following code might give you a key what to do:
    #!perl -w use strict; my (%variable) = ( 'EngineConfigDir' => 'c:\Daily\DreBa_Connect_Engine +\Config'); &SetSystemVariable(\%variable); # ------------------------------------------------------------ sub SetSystemVariable { my ($variable) = @_; # well, this should be in the head of your script # I just put it here to complete the function use Win32::TieRegistry( ArrayValues=>0 ); my $pound= $Registry->Delimiter("/"); my $systemVariable = $Registry->{"LMachine/SYSTEM/CurrentControlSet/Control/Session Man +ager/Environment"} or die ("Can't find root\n");; # set systemvariable foreach (keys %$variable){ $systemVariable->{"/$_"} = $variable->{$_}; } my $systemVariable2 = $Registry->{"LMachine/SYSTEM/CurrentControlSet/Control/Session Man +ager/Environment"} or die ("Can't find\n");; foreach my $key (keys %$systemVariable2){ print ("$key => $systemVariable->{$key}\n") if (grep { $key eq + "/$_" } keys %$variable); } } # SetSystemVariable # ------------------------------------------------------------
    I used it about two years ago to set an environment variable called EngineConfigDir. But I'm afraid a little bit more error handling is necessary.

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      I tried your code (as I have already written) and I found some strange things:
    • this code adds the keys to the registry
    • the variables can be seen in the "Variables d'environment" context menu
    • but if i type echo %The_variable_I_changed% i get just %The_variable_I_changed% and not the value
    • in %ENV there is no new key %The_variable_I_changed%

      strange, or?

      Anyone can help?

      ----
      NaSe
      :x

        Try to close the dos-prompt (or cmd.exe or whatever) and reopen it. If that still doesn't work, try to logoff and logon again (or just reboot).

        If the first solution fixes the problem, it might help to add the following line to the code above:

        $ENV{'The_variable_I_changed'} = 'value';
        This will temporarily add the environmentvariable, but will be gone if you close the shell. But if you reopen another, the system variable from the other code now will be accessible instead.

        Sorry, I can't test it because here I don't have access to a Windows-machine in the moment...

        Best regards,
        perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

      thanks a lot this is exactly what i was searching for

      ----
      NaSe
      :x

(tye)Re: Setting and changing system variables under win2000
by tye (Sage) on Aug 13, 2002 at 14:27 UTC

    For another example of how to do this, you can check out CleanPath.

    Note that strat's code in this thread will likely break %PATH% if you use it to change %PATH% (because it doesn't specify a "type" for the value which means a REG_SZ value will be set instead of a REG_EXPAND_SZ value which means you'll probably have unexpanded instances of %SystemRoot%). Luckilly, CleanPath will probably be able to fix it.

            - tye (but my friends call me "Tye")