当前位置:网站首页>[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 .

边栏推荐
- matlab保存DB4i深度相机图片
- Warm congratulations on the successful listing of five elements hehe liquor
- [programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
- [R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
- C# Newtonsoft. Use of job in JSON
- Caesar
- Basic number theory -- combinatorial number
- 2022电工(中级)复训题库及答案
- [MySQL learning notes 28] storage function
- 2022茶艺师(初级)操作证考试题库及模拟考试
猜你喜欢

Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?

How relational databases work

如何使用layui将数据库中的数据以表格的形式展现出来

【mysql学习笔记25】sql语句优化

Todolist classic case ①

Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)
![[programming training] delete public characters (hash mapping) + team competition (greedy)](/img/cd/63eb9da1e8956df0763797f079b67f.png)
[programming training] delete public characters (hash mapping) + team competition (greedy)

Minecraft 1.16.5 module development (51) tile entity

Basic knowledge of MATLAB

【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)
随机推荐
2022年流动式起重机司机考试练习题及在线模拟考试
【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)
Kickback -- find the first palindrome character in a group of characters
Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)
华为ModelArts训练Alexnet模型
继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?
Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
2022 test question bank and simulation test of tea master (primary) operation certificate
[R language] two /n data merge functions
[Shenzhen IO] precise Food Scale (some understanding of assembly language)
How do the top ten securities firms open accounts? In addition, is it safe to open a mobile account?
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
Detailed explanation of weback5 basic configuration
2022茶艺师(初级)操作证考试题库及模拟考试
微软宣布开源 (GODEL) 语言模型聊天机器人
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
Software testing methods and techniques - overview of basic knowledge
2022 mobile crane driver test exercises and online simulation test
[软件] phantomjs屏幕截图
【微服务|openfeign】Feign的日志记录