SETLOCAL: Begins localization of environment changes in a batch file.


 


... The examples for the command "SETLOCAL"
... "SETLOCAL" Excerpt from Microsoft Windows Help
... Important information, tips for the "SETLOCAL" command

The command: "SETLOCAL" is on Windows 12, 11, 10, .. , MS Server 2025, 2022, 2019, .. available

The examples for the command "SETLOCAL"

The `SETLOCAL` command in the Windows Command Prompt is used to set environment variables locally in a batch file. Here are some examples of using `SETLOCAL`: Example 1: Set local environment variable:

@ECHO OFF
SETLOCAL
SET LOCALE_VARIABLE=Content
ECHO %LOCAL_VARIABLE%

Description: In this example, the environment variable `LOKALE_VARIABLE` is set within the local scope (by `SETLOCAL` and `ENDLOCAL`) and can only be used within this scope. Example 2: Reset to previous status:

@ECHO OFF
SET GLOBALE_VARIABLE=GlobalContent

SETLOCAL
SET LOKALE_VARIABLE=LocalContent
ECHO Local variable: %LOKALE_VARIABLE%
ECHO Global Variable: %GLOBALE_VARIABLE%

ENDLOCAL
ECHO Local variable (outside the local scope): %LOKALE_VARIABLE%

Description: Here the local environment variable `LOKALE_VARIABLE` is set and used within the locally limited scope. After `ENDLOCAL`, the script returns to the previous state and the local variable is no longer available outside the local scope. Example 3: Nesting SETLOCAL:

@ECHO OFF
SETLOCAL
SET VARIABLE1=Value1
ECHO Inner Range: %VARIABLE1%

SETLOCAL
SET VARIABLE2=Value2
ECHO Outer area: %VARIABLE1%, %VARIABLE2%

ENDLOCAL
ECHO Out of local area: %VARIABLE1%, %VARIABLE2%

Description: Here the `SETLOCAL` command is nested twice. The variable `VARIABLE2` is only visible in the inner localized area, while `VARIABLE1` is visible in the outer and inner areas. Example 4: Enabling advanced environment variable processing:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET VARIABLE=Content
ECHO Before Delayed Expansion: %VARIABLE%
SET VARIABLE=NewContent
ECHO After Delayed Expansion: !VARIABLE!

Description: By using `ENABLEDELAYEDEXPANSION`, environment variables with an exclamation mark `!` instead of percent sign `%` can be used for delayed expansion. The `SETLOCAL` environment allows variables to be set locally for a batch file, whereby changes only apply in a limited area and do not affect the entire script globally. This is particularly useful if you want changes to environment variables to be limited within a specific section of the script.

"SETLOCAL" Excerpt from Microsoft Windows Help

Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.

C:\\WINDOWS>

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.  When the end
of a batch script is reached, an implied ENDLOCAL is executed for any
outstanding SETLOCAL commands issued by that batch script.

SETLOCAL

If Command Extensions are enabled SETLOCAL changes as follows:

SETLOCAL batch command now accepts optional arguments:
        ENABLEEXTENSIONS / DISABLEEXTENSIONS
            enable or disable command processor extensions.  See
            CMD /? for details.
        ENABLEDELAYEDEXPANSION / DISABLEDELAYEDEXPANSION
            enable or disable delayed environment variable
            expansion.  See SET /? for details.
These modifications last until the matching ENDLOCAL command,
regardless of their setting prior to the SETLOCAL command.

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:

    VERIFY OTHER 2>nul
    SETLOCAL ENABLEEXTENSIONS
    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.

Important information, tips for the "SETLOCAL" command

Yes, there are some important aspects to keep in mind when using the `SETLOCAL` command in the Windows Command Prompt or in batch scripts: 1. Limited scope: `SETLOCAL` creates a limited scope for environment variables. Changes to environment variables within this scope do not affect the global environment or other localized scopes. Note that `SETLOCAL` and `ENDLOCAL` should be used in pairs.

    SETLOCAL
    REM... Here changes to environment variables are local
    ENDLOCAL
    
2. Nesting: You can nest `SETLOCAL` to create multiple localized scopes. However, each `SETLOCAL` should be ended with a corresponding `ENDLOCAL`.

    SETLOCAL
    REM Inner localized area
        SETLOCAL
        REM Even deeper localized area
        ENDLOCAL
    ENDLOCAL
    
3. Delayed Expansion: You can enable delayed expansion with `SETLOCAL ENABLEDELAYEDEXPANSION`. The variable is enclosed with `!` instead of `%`. Lazy expansion allows variables to be evaluated at runtime, not just when reading the batch file.

    SETLOCAL ENABLEDELAYEDEXPANSION
    SET VARIABLE=Content
    ECHO Before Delayed Expansion: %VARIABLE%
    SET VARIABLE=NewContent
    ECHO After Delayed Expansion: !VARIABLE!
    ENDLOCAL
    
4. Global environment variables: Changes to environment variables made within a `SETLOCAL` scope have no effect on global environment variables outside the scope.

    SET GLOBAL=GlobalContent
    SETLOCAL
    SET GLOBAL=LocalContent
    ECHO Within the local area: %GLOBAL%
    ENDLOCAL
    ECHO Outside local area: %GLOBAL%
    
5. Undoing changes: If you want to undo changes in a localized area, simply revert to the state before `SETLOCAL` using `ENDLOCAL`.

    SETLOCAL
    REM changes here
    ENDLOCAL
    REM Undo the changes
    
It is important to use `SETLOCAL` and `ENDLOCAL` carefully and in pairs to ensure that environment variables are limited correctly and have the expected effects.


Deutsch
English
Español
Français
Italiano
日本語 (Nihongo)
한국어 (Hangugeo)
汉语 (Hànyǔ)
Türkçe
Português
Português
Svenska
Norsk
Dansk
Suomi
Nederlands
Polski









Windows-10


... Windows 10 FAQ
... Windows 10 How To


Windows 10 How To


... Windows 11 How To
... Windows 10 FAQ



The command SETLOCAL - Begins localization of environment changes in a batch file.

HTTP: ... console/en/059.htm
0.077
17325
How to change the Home-Group Password on Windows-7?
How long does it take to install Windows 11?
Can I deactivate the new Windows 11 Explorer context menu?
Command to convert text to keyboard commands? (Txt2Key)
Set hotkey for the day difference calendar as a fast shortcut!
Several programs start with a keyboard shortcut!
Kann ich auf Windows 11 die Viren weiterhin wegballern?
Laufende Prozesse Übersicht in Procsess-KO!
Desktop-Kalender so einstellen, das er automatisch immer minimiert startet bei Windows?
For what four different duration of Benschmarks?



(0)