SETLOCAL – Windows CMD Command

Set options to control the visibility of environment variables in a batch file.

SETLOCAL on it’s own, usually at the start of a batch file, will begin localisation of Environment Variables.

Issuing a SETLOCAL command, the batch script will inherit all current variables from the master environment/session.

Issuing an ENDLOCAL command will restore any environment variables present before the SETLOCAL was issued.

If a batch script does not use SETLOCAL then all variables will be Global, i.e. visible and modifiable by other scripts.

Although global variables are easy to work with they are not good practice – for example if you have several batch scripts dealing with filenames (and these scripts are CALLing one another), the first script has a variable called _filename, the second script a different variable called file-name (a different name to avoid conflicting with the first script) a third script now needs something like file_name this quickly becomes very difficult to manage.

With local variables you are free to use the same variable names in multiple batch scripts – there is no conflict because the local variables are not visible to any other script.
Local Variables can be passed from one batch routine to another with the ENDLOCAL command.


Setting EnabledDelayedExpansion will cause each variable to be expanded at execution time rather than at parse time.


Setting DisabledDelayedExpansion will cause each variable to be expanded at parse time rather than at execution time, this is the default behaviour.

Overloading a variable:

SETLOCAL can be used more than once in the same batch file so that multiple values can be stored in the same Environment Variable. To keep track of variable definitions, pair each SETLOCAL with a corresponding ENDLOCAL.

SETLOCAL is limited to 32 active instantiations per CALL level. At the root level a script can have up to 32 active SETLOCAL, and then CALL a subroutine that gets its own allocation of up to 32 SETLOCAL, etc.

@Echo off
::Standard commission
Set _Commission=20
Echo Standard commission %_Commission%

::Premium commission
Set _Commission=30
Echo Premium commission %_Commission%

::back to Standard commission
Echo %_Commission%


When run from a batch file, SETLOCAL will always set an ERRORLEVEL.
If given a valid argument or no arguments, a new environment is created %ERRORLEVEL% = 0
If bad parameters given, %ERRORLEVEL% = 1

EnableExtensions / DisableExtensions

Command Extensions are enabled by default, there is rarely any need to disable them.

If Command Extensions are permanently disabled or if a script is running under the Windows 95 command processor then SETLOCAL ENABLEEXTENSIONS will not be able to restore them.

A batch file to warn if command extensions are not available (see forum thread):

SETLOCAL is an internal command.

You may also like...

2 Responses

  1. zoritoler imol says:

    Hi , I do believe this is an excellent blog. I stumbled upon it on Yahoo , i will come back once again. Money and freedom is the best way to change, may you be rich and help other people.

  2. zortilo nrel says:

    I?¦ve recently started a blog, the information you provide on this web site has helped me greatly. Thanks for all of your time & work.

Leave a Reply

Your email address will not be published. Required fields are marked *