#1 09 Aug 2018 17:18

kingtermite
Member
Registered: 28 Oct 2016
Posts: 17

Setlocal Question....Endlocal needed at end of file?

Is "Endlocal" needed at end of batch file?

I was working on a batch file a few months ago...and could have sworn I found a mention of an odd behavior when another batch file was called in a certain way or something like that which caused the exiting from the batch file not to end the localality. But now I can't find anything about it or remember well enough what it was to repro the case (as I thought I did back a few months ago).

So does anyone know of any specific cases in which you may get into trouble if you don't put an endlocal at the end of your batch file (that had a setlocal) when returning to the calling batch file (e.g. let the endlocal happen organically by script ending)?

Last edited by kingtermite (09 Aug 2018 21:44)

Offline

#2 15 Aug 2018 09:37

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: Setlocal Question....Endlocal needed at end of file?

I can’t think of a case where an `endlocal` at the end of the file, or a subroutine, would ever make a difference.

Implicit `endlocal`-ing will always occur to cancel out any amount of `setlocal`s that have taken place at the end of each scope.

It is, however, possible to write broken internal commands up in such a way that the current scope terminates without performing the right amount of implicit `endlocal`-ing (read more here and here). Explicitly doing `endlocal` at the end wouldn’t make a difference though as the line would never be reached in these cases.

Offline

Board footer

Powered by FluxBB