easy to understand example of "setlocal"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

easy to understand example of "setlocal"

#1 Post by Rileyh » 11 Sep 2011 20:48

Hi,
Can someone give me an easy to understand example of the "setlocal" command.
I looked it up on cmd and it is WAY to complicated....

Any support would be greatly appreciated.

Regards,
Rileyh

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: easy to understand example of "setlocal"

#2 Post by !k » 12 Sep 2011 10:01

Code: Select all

@echo off
set Var=GLOBAL
echo Var is %Var% &pause >nul

setlocal &echo : setlocal
set Var=local
echo Var is %Var% &pause >nul

setlocal &echo : setlocal
set Var=very local
echo Var is %Var% &pause >nul
endlocal &echo : endlocal

echo Var is %Var% &pause >nul
endlocal &echo : endlocal

echo Var is %Var% &pause >nul

?

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: easy to understand example of "setlocal"

#3 Post by nitt » 12 Sep 2011 17:46

Also, don't forget:

Code: Select all

setlocal enabledelayedexpansion
set var1=bob
echo !bob!


setlocal /?

Code: Select all

Begins localization of environment changes in a batch file.  Environment
changes made after SETLOCAL has been issued are local to the batch file.
ENDLOCAL must be issued to restore the previous settings.  When the end
of a batch script is reached, an implied ENDLOCAL is executed for any
outstanding SETLOCAL commands issued by that batch script.

SETLOCAL

If Command Extensions are enabled SETLOCAL changes as follows:

SETLOCAL batch command now accepts optional arguments:
        ENABLEEXTENSIONS / DISABLEEXTENSIONS
            enable or disable command processor extensions. These
            arguments takes precedence over the CMD /E:ON or /E:OFF
            switches. See CMD /? for details.
        ENABLEDELAYEDEXPANSION / DISABLEDELAYEDEXPANSION
            enable or disable delayed environment variable
            expansion. These arguments takes precedence over the CMD
            /V:ON or /V:OFF switches. See CMD /? for details.
These modifications last until the matching ENDLOCAL command,
regardless of their setting prior to the SETLOCAL command.

The SETLOCAL command will set the ERRORLEVEL value if given
an argument.  It will be zero if one of the two valid arguments
is given and one otherwise.  You can use this in batch scripts
to determine if the extensions are available, using the following
technique:

    VERIFY OTHER 2>nul
    SETLOCAL ENABLEEXTENSIONS
    IF ERRORLEVEL 1 echo Unable to enable extensions

This works because on old versions of CMD.EXE, SETLOCAL does NOT
set the ERRORLEVEL value. The VERIFY command with a bad argument
initializes the ERRORLEVEL value to a non-zero value.

Rileyh
Posts: 147
Joined: 01 Sep 2011 03:54
Location: Perth, Western Australia

Re: easy to understand example of "setlocal"

#4 Post by Rileyh » 12 Sep 2011 19:27

@!k
Thanks for the batch script but what was the purpose of it?
When I ran it it just showed me a few words like setlocal and var- it didn't show me HOW it worked.
What I meant in the topic was an example of HOW it worked.

But thanks anyway,
Rileyh

Post Reply