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.

1
  • 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. Commented Jun 3, 2016 at 12:07

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This won't affect the calling process, though. Unless it is Explorer.
@Joey That depends on whether the process listens for the WM_SETTINGSCHANGED event and acts on it right.
indeed, but to a first approximation Explorer is about the only process that does that.
0
catch {exec cmd /C "setX $variable_name $valueToSet"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.