Does "endlocal" close all instances of "setlocal"?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PiotrMP006
Posts: 31
Joined: 08 Sep 2017 06:10

Does "endlocal" close all instances of "setlocal"?

#1 Post by PiotrMP006 » 23 Jul 2023 07:54

Does "endlocal" close all instances of "setlocal"?

ex

Code: Select all

setlocal enabledelayedexpansion enableextensions
set name1=1

setlocal disabledelayedexpansion enableextensions
set name2=2

setlocal enabledelayedexpansion enableextensions
set name3=3

endlocal

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Does "endlocal" close all instances of "setlocal"?

#2 Post by OJBakker » 23 Jul 2023 08:33

Just add a few lines to your script and the answer is obvious.
Try the script below.

Code: Select all

setlocal enabledelayedexpansion enableextensions
set name1=1
setlocal disabledelayedexpansion enableextensions
set name2=2
setlocal enabledelayedexpansion enableextensions
set name3=3
set name
endlocal
set name
endlocal
set name

shodan
Posts: 89
Joined: 01 May 2023 01:49

Re: Does "endlocal" close all instances of "setlocal"?

#3 Post by shodan » 20 Aug 2023 21:36

To anyone curious but don't feel like testing, here is the answer

Code: Select all

D:\dev\work>endlocal-depth-tester.bat
D:\dev\work>setlocal enabledelayedexpansion enableextensions
D:\dev\work>set name1=1
D:\dev\work>setlocal disabledelayedexpansion enableextensions
D:\dev\work>set name2=2
D:\dev\work>setlocal enabledelayedexpansion enableextensions
D:\dev\work>set name3=3
D:\dev\work>set name
name1=1
name2=2
name3=3
D:\dev\work>endlocal
D:\dev\work>set name
name1=1
name2=2
D:\dev\work>endlocal
D:\dev\work>set name
name1=1

Post Reply