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. Storm系列(一)集群的安装配置

    安装前说明: 必须先安装zookeeper集群 该Storm集群由三台机器构成,主机名分别为chenx01,chenx02,chenx03,对应的IP分别为192.168.1.110,192.168. ...

  2. 【解决】exited with a non-zero exit code 1

      安装好集群后运行hadoop-mapreduce-examples.jar 报错,比对cdh配置后,发现少了yarn.application.classpath,conf目录一定要配置,否则也会出 ...

  3. HW5.19

    public class MyTriangle { public static boolean isValid(double side1, double side2, double side3) { ...

  4. HW3.9

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  5. php5.5 yum源

    PHP官网在下载页面中只有源代码下载,当然使用编译安装也是可以的,但是使用二进制包安装会非常快速.虽然PHP官网没有提供二进制安装包,但是它推荐了两个第三方的YUM源(CentOS中默认的YUM安装的 ...

  6. light oj 1294 - Positive Negative Sign

    1294 - Positive Negative Sign   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  7. 第三周作业、实时操作系统µC/OS介绍及其它内容

    作业要求 见<实时控制软件设计>第三周作业 1 阅读笔记--µC/OS 1.1 基本介绍 µC/OS是由Micrium公司研发的实时操作系统,以µC/OS-II或µC/OS-III为内核, ...

  8. ios调打电话代码

    // 定义点击拨号按钮时的操作 - (void)callAction{ NSString *number = @"";// 此处读入电话号码 // NSString *num = ...

  9. Windows Mobile 6.0 SDK和中文模拟器下载

    [转] Windows Mobile 6.0 SDK和中文模拟器下载 Windows Mobile 6.5 模拟器 2010年12月06日 星期一 07:48 转载自 zhangyanle86 终于编 ...

  10. MVC - 身份验证

    FormsAuthenticationTicket  使用此类来为用户生成一个身份票据 持有该票据则说明该用户是通过了身份验证的用户 可以随时访问某些资源 我们先创建几个类 //用户 public c ...