The command: "ENDLOCAL" is on Windows 12, 11, 10, .. , MS Server 2025, 2022, 2019, .. available
The examples for the command "ENDLOCAL"
The
`ENDLOCAL` command is used in the Windows Command Prompt to terminate local environment variables initiated with the
`SETLOCAL` command. Here are some examples:
Example 1: Start and stop local environment:
@ECHO OFF
SETLOCAL
SET Name=John
ECHO Within the local environment: %Name%
ENDLOCAL
ECHO Outside the local environment: %Name%
Description: This example starts a local environment with
`SETLOCAL`, defines a local variable (
`name`), prints the value inside and outside the local environment, and then ends the local environment with
`ENDLOCAL`.
Example 2: Using ENDLOCAL in a loop:
@ECHO OFF
FOR /L %%A IN (1, 1, 3) DO (
SETLOCAL
SET Counter=%%A
ECHO Inside the loop: %Counter%
ENDLOCAL
)
ECHO Out of loop: %Counter%
Description: Here
`SETLOCAL` is used inside a loop to create a local environment.
`ENDLOCAL` then terminates the local environment at the end of each loop iteration.
Example 3: Local environment with return value:
@ECHO OFF
CALL :LocalFunction
ECHO Outside function: %ReturnValue%
GOTO :EOF
:LocalFunction
SETLOCAL
SET ReturnValue=This is a return value
ENDLOCAL & SET ReturnValue=%ReturnValue%
GOTO :EOF
Description: Here a function (
`:LocalFunction`) is created that starts a local environment with
`SETLOCAL` and ends with
`ENDLOCAL`. The return value is used outside the function.
Example 4: Using ENDLOCAL in a batch file:
@ECHO OFF
CALL :Start
ECHO Outside function: %GlobalVariable%
GOTO :EOF
:Begin
SETLOCAL
SET GlobalVariable=This is a global variable
CALL :InnerFunction
ENDLOCAL & SET GlobalVariable=%GlobalVariable%
GOTO :EOF
:InnerFunction
SET LocalVariable=This is a local variable
ECHO Inside the function: %LocalVariable%
GOTO :EOF
Description: Here
`SETLOCAL` is used in the main function (
`:Start`) to create a local environment. In this function a subfunction (
`:InnerFunction`) is called.
`ENDLOCAL` is used in the main function to exit the local environment while updating the value of the global variable.
The
`ENDLOCAL` statement terminates the local environment and resets the environment variables to the value before the
`SETLOCAL` command. If you want to use an environment variable from the local environment outside of it, you should do so directly after
`ENDLOCAL`. Note that local variables are not available outside of their local environment.
"ENDLOCAL" Excerpt from Microsoft Windows Help
Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.
C:\\WINDOWS>
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.
ENDLOCAL
If Command Extensions are enabled ENDLOCAL changes as follows:
If the corresponding SETLOCAL enable 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.
Important information, tips for the "ENDLOCAL" command
There are a few important points to note when using the
`ENDLOCAL` command in the Windows Command Prompt:
1.
Local Variables: `ENDLOCAL` terminates a local environment and all local variables created in that environment are discarded. These variables are no longer available outside the localized environment after
`ENDLOCAL`.
2.
Return Values: If you want to use values ??from a local environment outside of this, you must do so immediately after the
`ENDLOCAL` command. Example:
SETLOCAL
SET LocalVar=Hello
ENDLOCAL & SET OutsideVar=%LocalVar%
3.
Nested `SETLOCAL`/`ENDLOCAL`: If you use
`SETLOCAL` multiple times in a batch file, each
`SETLOCAL` should be followed by a corresponding
`ENDLOCAL`. Each
`ENDLOCAL` terminates the next
`SETLOCAL` statement.
4.
Variable inheritance: Variables created in a localized environment are not inherited to parent environments. Local environments are usually only visible to the current batch process.
5.
Execution order: `ENDLOCAL` is executed in the order in which
`SETLOCAL` was called. This means that the last localized environment will be terminated first with
`ENDLOCAL`.
6.
Batch files and functions: In batch files, localized environments are often used in functions. When exiting a function, an
`ENDLOCAL` is automatically carried out, unless the function was exited with
`GOTO :EOF` or
`EXIT /B`. In this case it is advisable to manually add an
`ENDLOCAL`.
7.
Use in loops: If you use
`SETLOCAL` in a loop, a new local environment will be created each time the loop runs. Be sure to place
`ENDLOCAL` correctly to ensure local variables are released as needed.
FOR /L %%A IN (1, 1, 3) DO (
SETLOCAL
SET LocalVar=Hello
ECHO %LocalVar%
ENDLOCAL
)
8.
Process Resources: Using
`SETLOCAL` and
`ENDLOCAL` can help conserve process resources, especially when many variables are defined and used in a batch file. Limiting the scope of variables to specific sections of code allows memory resources to be used more efficiently.
In summary, it is important to use
`SETLOCAL` and
`ENDLOCAL` carefully to ensure that variables are handled properly and localized environments are managed correctly. This is particularly relevant in complex batch scripts or functions.