SETLOCAL
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的更多相关文章
- setlocal enabledelayedexpansion
http://www.jb51.net/article/29323.htm 例1: 代码如下: @echo off set a=4 set a=5&echo %a% pause 结果:4 解 ...
- bat脚本-set(setlocal enabledelayedexpansion) 学习
设置本地为延迟扩展.其实也就是:延迟变量,全称延迟环境变量扩展. 事件一: @echo off set a=4 set a=5&echo %a% pause 解说:为什么是4而不是5呢?在ec ...
- 变量延迟(setlocal)之浅见
变量延迟,浅见认为就是变量预处理,在事先声明变量,告诉cmd环境哪个先哪个后.默认情况下是停用,可以用两种方法启用/停用: 一.cmd /v:on 和cmd /v:off ,范围在cmd这个环境直至e ...
- setlocal 与 变量延迟
setlocal 与 变量延迟 本条内容引用[英雄出品]的批处理教程: 要想进阶,变量延迟是必过的一关!所以这一部分希望你能认真看. 为了更好的说明问题,我们先引入一个例子.例1: @echo off ...
- setlocal enabledelayedexpansion 解释
看字面的意思是:设置本地为延迟扩展.其实也就是:延迟变量,全称"延迟环境变量扩展", 在cmd执行命令前会对脚本进行预处理,其中有一个过程是变量识别过程,在这个过程中,如果有两个% ...
- setlocal 本地变量详解
命令 setlocal (开启本地变量) endlocal (结束本地变量) 很多新手不理解这句话是什么意思,在批处理中有什么作用. 其实在批处理中 setlocal 作用很大,配合 endloca ...
- 批处理中setlocal enabledelayedexpansion的含义
setlocal enabledelayedexpansion 延迟变量全称"延迟环境变量扩展",要理解这个东西,我们还得先理解一下什么叫扩展! CMD在解 ...
- setlocal启动批处理文件中环境变量的本地化
setlocal启动批处理文件中环境变量的本地化 在执行 SETLOCAL 之后所做的环境改动只限于批处理文件.要还原原先的设置,必须执行 ENDLOCAL. 学习了:https://baike.ba ...
- CMake编译的VS工程,安装时遇到错误:error MSB3073: 命令“setlocal
错误提示 70>CMake Error at src/base/cmake_install.cmake:63 (file): 70> file INSTALL cannot find 70 ...
随机推荐
- HW5.25
public class Solution { public static void main(String[] args) { System.out.println(convertMillis(55 ...
- POJ2891 - Strange Way to Express Integers(模线性方程组)
题目大意 求最小整数x,满足x≡a[i](mod m[i])(没有保证所有m[i]两两互质) 题解 中国剩余定理显然不行....只能用方程组两两合并的方法求出最终的解,刘汝佳黑书P230有讲~~具体证 ...
- console.read()读入的内容
今天写的特别简单的代码,大体是一个模式选择,从控制台读入一个数,然后做出相应的选择. 代码如下: using System; using System.Collections.Generic; usi ...
- ARM MIPS PowerPC比较
ARM, MIPS, Power PC的比较 “ARM面向的低端消费类市场,拼的是功耗:PowerPC面向的是中高端市场,比的是性能,好像还没看到谁的手机是PowerPC的,也没有看到谁家企业级以 ...
- 极客技术专题【008期】:CSS3核心技术:选择器
日期:2013-8-19 来源:GBin1.com 技术专题介绍 技术专题:CSS3核心技术:选择器 专题演讲稿:CSS3选择器 分享人:知名前端技术博客 - w3cplus.com 博主 - 大漠 ...
- 1514:数值的整数次方 @jobdu
题目1514:数值的整数次方 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:377 解决:103 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent. ...
- 启用PowerShell Web Access
Windows PowerShell Web Access(PSWA)是 Windows Server 2012 中的新功能,充当 Windows PowerShell 网关,允许远程计算机基于 We ...
- 安装Rational Rose启动报错:无法启动此程序,由于计算机中丢失 suite objects.dll。
执行Rational Rose的时候假设出现这种错误,先检查环境变量有没有common的地址,假设没有直接配上就OK:配置例如以下:D:\Program Files\Rational\Common; ...
- 为Windows 7的winsxs目录瘦身,谨慎。
刚使用Win7 系统不久,前段时间在清理系统垃圾时发现,win7系统的windows文件夹下的winsxs 文件夹占用空间很大,想清理之,却提示无权限无法清理.随即在网上查了个到底,原来winsxs是 ...
- vim: 自己定义颜色主题(colorscheme)
命令模式下输入 :hi[ghlight] 能够看到当前vim的着色风格 命令模式下输入 :sy[ntax] 能够看到当前语法加亮模式,当文档的文字与列表里的正則表達式匹配时,vim会给文字着色.应用缩 ...