SETLOCAL continues after batch termination!
Posted: 08 Jun 2011 16:30
OK - I think this is kind of scary
It is possible for a SETLOCAL within a batch file to remain in effect even after the batch file terminates! If two SETLOCAL are in effect and a fatal error occurs (any fatal error?), only the last SETLOCAL is ended. The first one remains in effect even after batch termination I wasted many hours trying to diagnose why I was getting screwy results. For a while I thought I was going crazy!
test.bat
Interactive session:
Dave Benham
It is possible for a SETLOCAL within a batch file to remain in effect even after the batch file terminates! If two SETLOCAL are in effect and a fatal error occurs (any fatal error?), only the last SETLOCAL is ended. The first one remains in effect even after batch termination I wasted many hours trying to diagnose why I was getting screwy results. For a while I thought I was going crazy!
test.bat
Code: Select all
@echo off
setlocal DisableExtensions EnableDelayedExpansion
set test=AFTER 1st SETLOCAL
setlocal
set test=AFTER 2nd SETLOCAL
if =broken if
Interactive session:
Code: Select all
>set test=before 1st SETLOCAL
>set test
test=before 1st SETLOCAL
>echo !test!
!test!
>set /a 5+5
10
>test
if was unexpected at this time.
>set test
The syntax of the command is incorrect.
>echo !test!
AFTER 1st SETLOCAL
>set /a 5+5
The syntax of the command is incorrect.
Dave Benham