Home > Commands N-Z > Batch S

SETLOCAL


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

Begin localisation of environment variables in a batch file.


Syntax

SETLOCAL [[ENABLEEXTENSIONS] | [DISABLEEXTENSIONS] [[ENABLEDELAYEDEXTENSIONS] | [DISABLEDELAYEDEXTENSIONS]


Parameters
(DISABLEDELAYEDEXTENSIONS NT2000)
Will attempt to disable delayed environment variable expansion.
DISABLEEXTENSIONS (NT4)
Will attempt to disable Command extensions.
(ENABLEDELAYEDEXTENSIONS NT2000)
Will attempt to enable delayed environment variable expansion.
ENABLEEXTENSIONS (NT4)
Will attempt to re-enable Command extensions.

Switches

none.


Related

CMD
ENDLOCAL - End localisation of environment changes in a batch file.
SET
Equivalent Linux BASH commands:
readonly - Mark variables/functions as readonly.


Notes

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.


Examples

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)


Errorlevels
Availability
Internal
DOS
none
Windows
none
Windows NT
NT3.5 NT4 NT2000 NTXP NT2003