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. 检查REDO日志相关信息并生成HTML文件的脚本

    生成HTML格式的文件 内容有: 检查数据库版本.REDO日志组情况, 最近20次日志切换频率检查--日志间的归档时间间隔, 这对查看数据库的IO繁忙时段 统计指定日期当天每小时的归档日志产生量--日 ...

  2. 树莓派通过 HDMI - VGA 转接后分辨率始终为640*480无法修改的问题

    一开始装的Raspbian,感觉系统不错,就是分辨率调不了,网上找了很多解决方法,捣鼓了差不多一天,仍然没有解决. 期间尝试换了好几个系统,比如说 raspbmc .XBian等,最后试了下Pidor ...

  3. Dom4J对XML的创建、修改、删除等操作

    Dom4j也可以很方便完成XML文档的创建.元素的修改.文档的查询遍历等,但dom4j稍比jdom复杂一点,不过在大片文档的情况下dom4j的性能要不jdom好. # 准备 首先,提供相关的jar包 ...

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

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

  5. NLog使用说明

    NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码. NLog允许我们自定义从跟踪消息的来源(source)到记录跟踪信息的目标(target)的规则 ...

  6. zookeeper-3.4.8 集群搭建

    一.虚拟机环境 见我的另一篇博客http://www.cnblogs.com/xckk/p/6000881.html, zookeeper是用JAVA编写,因此需要安装JDK环境,centos下安装J ...

  7. 正则表达式_matches(Regex)

    [0-9a-zA-Z.%+-] 匹配中括号中的 0-9 或者 a-z 或者 A-Z 或者 . 或者 % 或者 + 或者 - $p = "111,222,333"$p -match ...

  8. 【Android UI设计与开发】之具体解释ActionBar的使用

    具体解释Android中的ActionBar的使用 请尊重他人的劳动成果,转载请注明出处:具体解释Android中的ActionBar的使用 http://blog.csdn.net/fengyuzh ...

  9. xcode针对不同IOS版本的代码编译问题

    有时候在项目中为了兼容低版本IOS系统,通常会针对不同的OS版本写不同的代码,例如: #define IS_IOS7_OR_LATER ([[UIDevice currentDevice].syste ...

  10. java程序查不出数据来

    同样的错误,不可再犯第三次!!! 数据库中是char,里面带空格,但在pl/sql中这样写可以查出来.如下: select ipostid from product t where ipostid= ...