0
votes

I have a Tcl script which generates some value which I like to store in a Windows environment variable. I know that this cannot be done directly from within my script.

What I am planning to do is to output the value to stderr:

puts stderr $var

How do I use the stderr channel to set a environment variable in the calling batch file?

Is there a more convenient way to set a environment variable from a Tcl script?

1

1 Answers

1
votes

The easiest way is probably to use the Tcl script to write a batch file with a known name that contains the environment variable setting, say env.bat:

set f [open env.bat w]
puts $f "SET FOO=BAR"
close $f

Then you can make your real batch file just CALL that to get the environment variable definition from it:

CALL env.bat

This is the method that is going to be easiest to make work in a way that is non-surprising. It's also going to be pretty easy to debug, since you can always look at env.bat in a text editor and figure out if the things in it are sensible. (I'm assuming that everything is run in the same directory and that the code has permission to write there; that's definitely the easiest way to do it. It's possible to write a temporary file somewhere else and pass the name around, but that's rather more complex to make function correctly.)