Begin localisation of environment variables in a batch file.
SETLOCAL [[ENABLEEXTENSIONS] | [DISABLEEXTENSIONS] [[ENABLEDELAYEDEXTENSIONS] | [DISABLEDELAYEDEXTENSIONS]
ENDLOCAL - End localisation of environment changes in a batch file.
Equivalent Linux BASH commands:
readonly - Mark variables/functions as readonly.
If Command Extensions are permanently disabled then SETLOCAL ENABLEEXTENSIONS will not restore them.
Changes made to an Environment Variable after SETLOCAL has been issued are local to the batch file. ENDLOCAL must be issued to restore the previous settings.
SETLOCAL can be used more than once in the same batch file so that multiple values can be stored in one Environment Variable.
@ECHO OFF :: ::Standard commission SET V_Commission=20 ECHO %V_Commission% :: ::Super commission SETLOCAL SET V_Commission=30 ECHO %V_Commission% :: ::Premium commission SETLOCAL SET V_Commission=40 ECHO %V_Commission% ENDLOCAL :: ::Back to Super commission ECHO %V_Commission% ENDLOCAL :: ::back to Standard commission ECHO %V_Commission%
SETLOCAL will set an ERRORLEVEL 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 a batch file to determine if command extensions are available, using this technique:
VERIFY errors 2>nul SETLOCAL ENABLEEXTENSIONS IF ERRORLEVEL 1 ECHO Unable to enable extensions
This works because "VERIFY errors" sets ERRORLEVEL to 1 and then the SETLOCAL will fail to reset the ERRORLEVEL value if extensions are not available (e.g. if the script is running under command.com)