WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Environment

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Windows Environment Variables


Question:

Are DOS/Windows environment variables allowed in WinBatch scripts?

i.e.

%temp%
%programfiles%
%windir%

Answer:

Yes. But not in that manner.

%temp% is a construct that we call a %substitution% in which the %temp% is replaced *anywhere* on the script code line by the contents of the temp variable. Kinda like how environment variables are handled in DOS, but it uses WinBatch variables,

Soooo What people do is pre-load the WinBatch temp variable from the Environment variable with the environment functions. As in

temp=Environment("TEMP") ;get ENV variable into WB variable
then you can %temp% as you wish as in
Message("Hi there","Temp env variable is set to %temp%")
HOWEVER. Ahem. For various in sundry reasons use of %substitution% in any but the most controlled circumstances is generally frowned upon.

It would be frowned upon in this case as the contents of the Environment variable temp are "uncontrolled". An excessively long temp variable could cause you script to error out with a "line too long" error (as %substitution% effectively edits the line before it is processed.

%substitution% turns out to be both a shining star and a festering hellhole of the WIL language. It allows amazing tricks to be done if used with great foresight and caution, but can easily lead into crashing scripts, especially during demonstrations to higher-ups if the proper care is not used. The general solution is to use StrCat(...) instead.

The recommended method would be

Message("Hi there",strcat("Temp env variable is set to ",temp))
If you are coding for yourself on a home machine, I guess %temp% would be fine. But if coding for 10000 network users, avoid %substitution% like the plague.


Article ID:   W16439
File Created: 2005:02:18:12:20:34
Last Updated: 2005:02:18:12:20:34