DosTips.com

A Forum all about DOS Batch
It is currently 09 Feb 2016 14:40

All times are UTC-06:00




Post new topic  Reply to topic  [ 4 posts ] 
Author Message
PostPosted: 08 Jun 2011 16:30 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1607
Location: United States (east coast)
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


Top
   
PostPosted: 17 Jul 2011 11:12 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1607
Location: United States (east coast)
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


Top
   
PostPosted: 18 Jul 2011 12:22 
Offline

Joined: 10 Jun 2011 10:30
Posts: 126
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...


Top
   
PostPosted: 12 Aug 2011 13:49 
Offline
Expert

Joined: 30 Aug 2007 08:05
Posts: 765
Location: Germany
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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 4 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], catalinnc, Yahoo [Bot] and 14 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® Forum Software © phpBB Limited