In a Windows batch script (.bat or .cmd), the commands setlocal and endlocal are used to create a local scope for environment variable changes.
This marks the beginning of a local scope.
After this point, any environment variable changes (with set, call set, etc.)
are only visible within the current script or block and will be reverted after endlocal is reached.
Think of it like opening a temporary environment context.
This ends the local scope started by setlocal,
discarding all changes made to environment variables inside that scope and restoring their previous values.
@echo off
set MY_VAR=global
echo Before setlocal: %MY_VAR%
setlocal
set MY_VAR=local
echo Inside setlocal: %MY_VAR%
endlocal
echo After endlocal: %MY_VAR%This outputs:
Before setlocal: global
Inside setlocal: local
After endlocal: global