当前位置:网站首页>[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
2022-07-01 07:48:00 【dssgresadfsrgre】
Reference
Batch command ——for - kaizen - Blog Garden
Bat Delay variable of script cmd /v:on_komomon‘s blog The blog of -CSDN Blog _bat Delay variable
One 、“ Delay variable ” The existence background of
The execution process of batch processing is “ From top to bottom , Implementation by item ”, and “ Implementation by item ” Do not represent “ Execute... Line by line ”.
Because one instruction may occupy multiple lines .
In compound statements , The whole compound statement is a complete statement , No matter how many lines the compound statement occupies . Common compound statements are :for sentence 、if……else sentence 、 Use connector &、|| and && Connected statement , Use the pipe symbol | Connected statement , And enclosed in parentheses 、 A statement block composed of multiple statements .
In non compound statements , If the statement occupies a line position , Then this line of code is a complete statement .
Batch processing is performed before executing a statement “ Preprocessing ”, The preprocessing process will identify all variables , Assign existing values to variables directly .
Even in a statement , Operations involving the same variable are preceded and followed , Changed the value of the variable before , This updated value cannot be passed to the following variables , Therefore, the latter variables are the old values when executing the command ( But often we want the updated value obtained by executing the previous command to be directly passed to the following variables ).
for example , You wrote a script file .
chcp 65001
@echo off
set school= Xiangtan University
echo " I am now studying %school%"
echo " After four years , I received the admission notice from Chongqing University "
set school= Chongqing University & echo I am going to study in %school%
pause>nulThe results are as follows . Isn't that weird ? What should we do according to reason , First put your own school Set as Chongqing University , The output should be “ I am going to study in Chongqing University now ” Well .

The reason is because of the order 【set school= Chongqing University & echo I am going to study in %school% 】 It's a compound statement , During pretreatment, the following school Variable has been assigned “ Xiangtan University ” 了 .
Is there an option or switch , After we opened it , Without changing the original sentence structure , Replace the variable after the compound statement with the latest value executed before .
Yes, there is ! Of course. ! What if you want to know what happened , Please listen to the following text !
Two 、 stay cmd Open the delay variable in the window
stay cmd Window , We carry out orders 【cmd /?】 You can get relevant help information , The following is a key quotation ( All other irrelevant items have been deleted , If you are interested in other parameters, you can print them by yourself ).
C:\Users\Administrator>cmd /?
start-up Windows A new instance of the command interpreter
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]
/V:ON Use ! Enable deferred environment variables as delimiters
Expand . for example ,/V:ON Will allow !var! When executed
Extended variables var.var The syntax will be used when inputting
Extended variables , This is different from FOR
Different within the cycle .
/V:OFF Disable deferred environment extensions .
Deferred environment variable extension is not enabled by default . you
It can be used /V:ON or /V:OFF switch , by CMD.EXE And
Enable or disable deferred environment variable extensions . you
Can be on the machine and / Or enable or disable... On the user login session CMD.EXE all
Deferred extension of the call , This should be set to use REGEDIT.EXE In the registry of
One or two REG_DWORD value :
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\DelayedExpansion
and / or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\DelayedExpansion
To 0x1 or 0x0. User specific settings
Has priority over the machine settings . Command line switch
Has priority over registry settings .
In a batch file ,SETLOCAL ENABLEDELAYEDEXPANSION or DISABLEDELAYEDEXPANSION
Parameter ratio /V:ON or /V:OFF The switch has priority . see also SETLOCAL /?
Get details .
If deferred environment variable extension is enabled ,
The exclamation point character can be used at execution time
Replace the value of an environment variable .We can know from the help information , There are roughly two ways to turn on the delay variable switch , Either way, you need to put the percentage sign on both sides of the variable % Change to exclamation point !.
The first one is , stay cmd Open delay variable in , It is also divided into temporary passing of orders 【cmd /v:on】 Or order 【cmd /v】 Open and permanently modify registry variables .
The second kind , Open the delay variable in the batch script , It is also divided into temporary passing of orders 【SETLOCAL ENABLEDELAYEDEXPANSION】 Open and permanently modify registry variables .
Let's try the first temporary modification .
Carry out orders 【cmd /v:on】 after , Then enter the delayed variable expansion environment ,cmd The command code is also added after the window title .

Then execute the order .
set CQ=1
set /a CQ+=1 & echo !CQ!The results are shown below ,set /a CQ+=1 The output of is 2, And the back echo It also outputs 2, Explain what follows echo The previous one is used set The latest value after execution .

We can order 【exit】 Or order 【cmd /v:off】 To exit the delayed variable state .
after , Then execute the above command , give the result as follows .

Let's continue to explain how to use the deferred variable mechanism in batch scripts .
3、 ... and 、 Open the delay variable in the batch script
Create a new one bat file , The code is as follows .
chcp 65001
@echo off
set school= Xiangtan University
echo " I am now studying %school%"
echo " After four years , I received the admission notice from Chongqing University "
set school= Chongqing University & echo I am going to study in %school%
echo Wrong output this time
echo " Open the delay variable and try "
setlocal EnableDelayedExpansion
set school= Xiangtan University
set school= Chongqing University & echo I am going to study in !school!
echo " It's finally right "
echo " Try closing the delay variable again "
setlocal DisableDelayedExpansion
set school= Xiangtan University
set school= Chongqing University & echo I am going to study in %school%
echo " Sure enough, I was wrong again "
pause>nulAfter performing , The output is as follows .

边栏推荐
- Rk3399 platform development series explanation (network debugging) 7.30. What will affect the sending process of TCP packets?
- How to create an exclusive vs Code theme
- 【R语言】两个/N个数据合并merge函数
- redisson使用全解——redisson官方文档+注释(上篇)
- Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
- MySQL and redis consistency solution
- Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
- ctfshow-web351(SSRF)
- 【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
- Inftnews | from "avalanche" to Baidu "xirang", 16 major events of the meta universe in 30 years
猜你喜欢

STM32 uses esp01s to go to the cloud, mqtt FX debugging

Félicitations pour l'inscription réussie de wuxinghe

kubernetes资源对象介绍及常用命令(二)

Conscience Amway universal wheel SolidWorks model material website

赌上了绩效,赢了公司CTO,我要搭DevOps平台!

Atguigu---- scaffold --02- use scaffold (2)

Array: question brushing record

redisson使用全解——redisson官方文檔+注釋(上篇)

AUTOSAR learning record (1) – ECUM_ Init

The database is locked. Is there a solution
随机推荐
Sorting out tcp/udp communication problems
Introduction to kubernetes resource objects and common commands (II)
【微服务|openfeign】Feign的日志记录
2022年茶艺师(中级)复训题库及答案
base64
微软宣布开源 (GODEL) 语言模型聊天机器人
熱烈祝賀五行和合酒成功掛牌
2022茶艺师(初级)操作证考试题库及模拟考试
ctfshow-web351(SSRF)
Detailed explanation of weback5 basic configuration
redisson使用全解——redisson官方文档+注释(中篇)
Redisson uses the full solution - redisson official documents + comments (Part 2)
TodoList经典案例①
MySQL and redis consistency solution
力扣——求一组字符中的第一个回文字符
weback5基础配置详解
[MySQL learning notes 25] SQL statement optimization
2022 tea master (intermediate) recurrent training question bank and answers
Do securities account opening affect the security of account opening
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched