0

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 Answer 1

1

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.)

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

1 Comment

Thanks Donal, your suggestion provides a generic solution for my issue. However, in my particular case, I only have a limited number of possible return values, so i decided to return from Tcl using the exit command and then evaluate the ERRORLEVEL in the caller batch file. Works fine!

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.