1
votes

I am working on a Tcl script in which I have a variable and I just want to keep its value alive after the completion of its execution, so I am trying to define a windows environment variable by executing the set windows command within the script using the exec function.

I have also tried to find the solution through google but that too didn't work. Here is the line of code which I have tried

exec set verName=$xVar

It would be great if you can help me. Thanks in advance.

2
set env(YOUR_VARIABLE) value will set the environmental variable and it is available only during the execution. i.e. It is temporary, not a permanent.Dinesh

2 Answers

2
votes

First, read this question and its accepted answer: Set a persistent environment variable from cmd.exe

How do you do that from Tcl? Well, the standard registry package gives you the tools:

package require registry

set root {HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment}
set theVariable "ABCDE"
set theValue "12345XYZ"

registry set $root\\$theVariable $theValue
registry broadcast "Environment"

The manual page for the registry package has a specific example for using this with the PATH. Updating to other variables is trivial. The script will need to be run in a session with Administrator privileges in order to update that part of the registry.

0
votes
catch {exec cmd /C "setX $variable_name $valueToSet"}