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 ...
随机推荐
- Esper系列(四)Output
功能 控制Esper事件流计算结果的输入形式.时间点及频率: 格式 1 ";
- [JSOI2008]星球大战
正向进行很显然很BT, 所以逆向离线此题是个不错的选择. 使用并查集统计劫难之后的联通状态, 然后逐个添加结点即可. #include <stdio.h> #include <std ...
- yum kvm
---恢复内容开始--- 1. 安装软件包: yum -y install virt-manager; yum install qemu-kvm yum install libvirt yum ...
- Topology拓扑
- 如何将可执行文件打包至APK并运行(转)
原文链接:http://www.lupaworld.com/home.php?mod=space&uid=345712&do=blog&id=248921 好久没有写bolg了 ...
- C#Winform窗口特效源码(1)
本文基于.Net开发,使用C#作为开发语言,分别包含以下效果: 移动无边框窗口.窗口移动限制(限制在屏幕内).桌面贴边自动隐藏(仿QQ隐藏窗口) 1.移动无边框窗口 采用了消息的方式,可以实现通过窗口 ...
- apache+php配置中遇到的问题
在安装apache+php配置中遇到的问题: LoadModule php5_module "D:/PHP/php5apache2_4.dll"PHPIniDir "D: ...
- jfinal文件上传和form表单值为null的解决方法
今天使用jfinal做上传提交的时候,遇到一个问题:添加了上传功能,原来的form表单submit提交时所有值都为null了,研究了很长时间,终于发现 在jfinal上传时候,jsp加 enctype ...
- WPF中的StackPanel、WrapPanel、DockPanel
一.StackPanel StackPanel是以堆叠的方式显示其中的控件 1.可以使用Orientation属性更改堆叠的顺序 Orientation="Vertical" 默认 ...
- 【Android】 onSaveInstanceState()恢复数据
onSaveInstanceState()方法会携带一个 Bundle 类型的参数,Bundle 提供了一系列的方法用于保存数据,比如可以使用 putString()方法保存字符串,使用 putInt ...