Rob van der Woude's Scripting Pages

NT's and OS/2's


The syntax given below is for Windows NT 4.
In OS/2 (CMD.EXE) SETLOCAL and ENDLOCAL follow the same basic syntax, without the extensions of NT's Command Extensions.


Note: The parts of this text that are displayed in magenta are valid for Windows 2000 and later versions only



Begins localization of environment changes in a batch file.
Environment changes made after SETLOCAL has been issued are local to the batch file.
ENDLOCAL must be issued to restore the previous settings.


In Windows NT 4 and later versions, if Command Extensions are enabled, SETLOCAL changes as follows:

SETLOCAL batch command now accepts an optional argument, which can be either ENABLEEXTENSIONS or DISABLEEXTENSIONS.
This will enable or disable the Command process extensions until the matching ENDLOCAL command, regardless of their setting prior to the SETLOCAL command.

In Windows 2000, an additional pair of switches is available:


enable or disable delayed environment variable expansion (see SET /?).

The SETLOCAL command will set the ERRORLEVEL value if given an argument.
It will be zero if one of the two valid arguments is given and one otherwise.
You can use this in batch scripts to determine if the extensions are available, using the following technique:

	IF ERRORLEVEL 1 echo Unable to enable extensions

This works because on old versions of CMD.EXE, SETLOCAL does not set the ERRORLEVEL value.
The VERIFY command with a bad argument initializes the ERRORLEVEL value to a non-zero value.



Ends localization of environment changes in a batch file.
Environment changes made after ENDLOCAL has been issued are not local to the batch file; the previous settings are not restored on termination of the batch file.


In Windows NT 4 and 2000, if Command Extensions are enabled, ENDLOCAL changes as follows:

If the corresponding SETLOCAL enabled or disabled command extensions using the new ENABLEEXTENSIONS or DISABLEEXTENSIONS options, then after the ENDLOCAL, the enabled/disabled state of command extensions will be restored to what it was prior to the matching SETLOCAL command execution.



Sometimes it would be nice if we could use SETLOCAL and ENDLOCAL to preserve the initial environment but still change one variable "permanently", in the SORTDATE and SORTTIME examples, for example.

I recently saw a posting at the alt.msdos.batch.nt newsgroup where Phil Robyn solved this problem in an ingenious way:

:: Variable test is set within local environment, which means
:: its changes are flushed by the next ENDLOCAL command
SET TEST=Some new value
:: By using the ampersand and the following SET command on the
:: same line as the ENDLOCAL command, %TEST% is resolved before
:: the ENDLOCAL command "restores" its value

This way the environment variable TEST and its value are preserved in spite of the ENDLOCAL command.
See my Conditional Execution page for an explanation of the ampersand's usage.

page last modified: 2021-01-27