1

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.

stark
  • 2,246
  • 2
  • 23
  • 35
anurag
  • 202
  • 3
  • 12
  • `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 Jun 03 '16 at 12:07

2 Answers2

2

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.

Community
  • 1
  • 1
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
0
catch {exec cmd /C "setX $variable_name $valueToSet"}
Ulysses
  • 5,616
  • 7
  • 48
  • 84