View unanswered posts | View active topics It is currently 21 Dec 2014 08:22



Post new topic Reply to topic  [ 4 posts ] 
SETLOCAL continues after batch termination! 
Author Message
Expert

Joined: 12 Feb 2011 21:02
Posts: 1314
Location: United States (east coast)
Post SETLOCAL continues after batch termination!
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 :!: :shock: I wasted many hours trying to diagnose why I was getting screwy results. :roll: For a while I thought I was going crazy!

test.bat
Code:
@echo off
setlocal DisableExtensions EnableDelayedExpansion
set test=AFTER 1st SETLOCAL
setlocal
set test=AFTER 2nd SETLOCAL
if =broken if

Interactive session:
Code:
>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


08 Jun 2011 16:30
Profile
Expert

Joined: 12 Feb 2011 21:02
Posts: 1314
Location: United States (east coast)
Post Re: SETLOCAL continues after batch termination!
I found another situation where SETLOCAL persists after script termination: Whenever a fatal syntax error occurs within a CALLed routine while command extensions are enabled :!:

test.bat
Code:
@echo off
setlocal EnableDelayedExpansion
set test=AFTER SETLOCAL
call :SyntaxError
exit /b

:SyntaxError
for

Interactive session:
Code:
>set test=BEFORE SETLOCAL

>echo %test%
BEFORE SETLOCAL

>echo !test!
!test!

>test
The syntax of the command is incorrect.

>echo !test!
AFTER SETLOCAL

>


Dave Benham


17 Jul 2011 11:12
Profile

Joined: 10 Jun 2011 10:30
Posts: 126
Post Re: SETLOCAL continues after batch termination!
What's even worse is you have to close out of the command session to get rid of them. I was unable to get an endlocal command to get rid of my variables...


18 Jul 2011 12:22
Profile
Expert

Joined: 30 Aug 2007 08:05
Posts: 679
Location: Germany
Post Re: SETLOCAL continues after batch termination!
Acy Forsythe wrote:
What's even worse is you have to close out of the command session to get rid of them. I was unable to get an endlocal command to get rid of my variables...


Yes this is interesting, what happens internally?

And it seems to be the only way to change the delayed expansion mode (and the extension mode) for the current cmd instance!

jeb


12 Aug 2011 13:49
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], siberia-man, Yahoo [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.