Quote from: http://ss64.com/nt/setlocal.html

SETLOCAL

Set options to control the visibility of environment variables in a batch file.

Syntax
SETLOCAL SETLOCAL EnableDelayedExpansion SETLOCAL EnableExtensions SETLOCAL DisableExtensions SETLOCAL EnableExtensions EnableDelayedExpansion Key
EnableDelayedExpansion Expand variables at execution time rather than at parse time. EnableExtensions Attempt to enable Command extensions. DisableExtensions Attempt to disable Command extensions.

SETLOCAL on it's own, usually at the start of a batch file, will begin localisation of Environment Variables.

Issuing a SETLOCAL command, the batch script will inherit all current variables from the master environment/session.

Issuing an ENDLOCAL command will restore any environment variables present before the SETLOCAL was issued.

If a batch script does not use SETLOCAL and ENDLOCAL then all variables will be Global, i.e. visible and modifiable by other scripts.

Although global variables are easy to work with they are not good practice - for example if you have several batch scripts dealing with filenames (and these scripts may be CALLing one another), the first script may have a variable called _filename, the second script a different variable called file-name (a different name to avoid conflicting with the first script) a third script now needs something like file_name this quickly becomes very difficult to manage.

With local variables you are free to use the same variable names in multiple batch scripts - there is no conflict because the local variables are not visible to any other script. 
Local Variables can be passed from one batch routine to another with the ENDLOCAL command.

EnableDelayedExpansion

Setting EnabledDelayedExpansion will cause each variable to be expanded at execution time rather than at parse time.
EnableDelayedExpansion is Disabled by default.

Overloading a variable:

SETLOCAL can be used more than once in the same batch file so that multiple values can be stored in the same Environment Variable. To keep track of variable definitions, pair each SETLOCAL with a corresponding ENDLOCAL.

SETLOCAL is limited to 32 active instantiations per CALL level. At the root level a script can have up to 32 active SETLOCAL, and then CALL a subroutine that gets its own allocation of up to 32 SETLOCAL, etc.

@Echo off 
SETLOCAL
::Standard commission
Set _Commission=20 
Echo Standard commission %_Commission%

::Premium commission
SETLOCAL 
Set _Commission=30
Echo Premium commission %_Commission%

::back to Standard commission
ENDLOCAL
Echo %_Commission%

ENABLEEXTENSIONS / DISABLEEXTENSIONS

Command Extensions are enabled by default, there is rarely any need to disable them.

If Command Extensions are permanently disabled or if a script is running under the Windows 95 command processorcommand.com then SETLOCAL ENABLEEXTENSIONS will not be able to restore them.

A batch file to warn if command extensions are not available:

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

Errors

When run from a batch file, SETLOCAL will always set an ERRORLEVEL.
if given a valid argument or no arguments, the %errorlevel% will be set to zero, otherwise it will be set to 1.

SETLOCAL is an internal command.

"A local shop for local people" - The League Of Gentlemen

SETLOCAL的更多相关文章

  1. setlocal enabledelayedexpansion

    http://www.jb51.net/article/29323.htm 例1: 代码如下: @echo off set a=4 set a=5&echo %a% pause  结果:4 解 ...

  2. bat脚本-set(setlocal enabledelayedexpansion) 学习

    设置本地为延迟扩展.其实也就是:延迟变量,全称延迟环境变量扩展. 事件一: @echo off set a=4 set a=5&echo %a% pause 解说:为什么是4而不是5呢?在ec ...

  3. 变量延迟(setlocal)之浅见

    变量延迟,浅见认为就是变量预处理,在事先声明变量,告诉cmd环境哪个先哪个后.默认情况下是停用,可以用两种方法启用/停用: 一.cmd /v:on 和cmd /v:off ,范围在cmd这个环境直至e ...

  4. setlocal 与 变量延迟

    setlocal 与 变量延迟 本条内容引用[英雄出品]的批处理教程: 要想进阶,变量延迟是必过的一关!所以这一部分希望你能认真看. 为了更好的说明问题,我们先引入一个例子.例1: @echo off ...

  5. setlocal enabledelayedexpansion 解释

    看字面的意思是:设置本地为延迟扩展.其实也就是:延迟变量,全称"延迟环境变量扩展", 在cmd执行命令前会对脚本进行预处理,其中有一个过程是变量识别过程,在这个过程中,如果有两个% ...

  6. setlocal 本地变量详解

    命令 setlocal (开启本地变量)  endlocal (结束本地变量) 很多新手不理解这句话是什么意思,在批处理中有什么作用. 其实在批处理中 setlocal 作用很大,配合 endloca ...

  7. 批处理中setlocal enabledelayedexpansion的含义

    setlocal enabledelayedexpansion       延迟变量全称"延迟环境变量扩展",要理解这个东西,我们还得先理解一下什么叫扩展!       CMD在解 ...

  8. setlocal启动批处理文件中环境变量的本地化

    setlocal启动批处理文件中环境变量的本地化 在执行 SETLOCAL 之后所做的环境改动只限于批处理文件.要还原原先的设置,必须执行 ENDLOCAL. 学习了:https://baike.ba ...

  9. CMake编译的VS工程,安装时遇到错误:error MSB3073: 命令“setlocal

    错误提示 70>CMake Error at src/base/cmake_install.cmake:63 (file): 70> file INSTALL cannot find 70 ...

随机推荐

  1. Kafka系列(二)特性和常用命令

    Kafka中Replicas复制备份机制 kafka将每个partition数据复制到多个server上,任何一个partition有一个leader和多个follower(可以没有),备份的个数可以 ...

  2. Hadoop概念学习系列之如何去找到历史版本的Hadoop发行包(三十四)

    如何去找到历史版本的Hadoop发行包 找到Hadoop历史版本 这里我需要的Hadoop版本是2.0.3.打开hadoop的下载页面 http://www.apache.org/dyn/closer ...

  3. IntelliJ IDEA的下载、安装和WordCount的初步使用(本地模式和集群模式)

    包括: IntelliJ IDEA的下载  IntelliJ IDEA的安装 IntelliJ IDEA中的scala插件安装 用SBT方式来创建工程 或 选择Scala方式来创建工程 本地模式或集群 ...

  4. log4j输出信息到mongodb

    官网     http://log4mongo.org/display/PUB/Log4mongo+for+Java 保存主机信息,方法等      http://my.oschina.net/chi ...

  5. flot图插件使用

    <div id="budget-charts"></div> <script src="../../../plugins/ace/js/fl ...

  6. jquery判断文本域长度

    <div id="result">您还可以输入100字</div> <textarea name="content" id=&qu ...

  7. ASP.NET MVC- JSON ,Jquery, State management and Asynch controllers

    一.JSON  MVC And JQuery In case you are new to JSON please read this before moving ahead with this la ...

  8. Unrecognized Windows Sockets error: 0: JVM_Bind 异常解决办法

    java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind 此异常的原因是服务器端口被占用 所以解决办法是: 一 ...

  9. sping获取bean方法 解决资源耗尽

    // ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationCon ...

  10. OpenOffice的安装与启动

    一.安装openOffice1.使用tar -xzvf OOo_3.2.0_LinuxIntel_install_wJRE_en-US.tar.gz解压缩后,会得到OOO320_m12_native_ ...