Windows批处理文件:什么是变量扩展,又是什么意思EnableDelayedExpansion? [英] Windows Batch files: what is variable expansion, and what does EnableDelayedExpansion mean?
本文介绍了Windows批处理文件:什么是变量扩展,又是什么意思EnableDelayedExpansion?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是变量扩展是什么意思?这是否意味着简单的变量定义,还是别的什么?
What is meant by "variable expansion"? Does it mean simply "variable definition", or something else?
当我说会发生什么 SETLOCAL EnableDelayedExpansion
?谷歌不明确。
What happens when I say setLocal EnableDelayedExpansion
? Google wasn't clear.
推荐答案
- 变量的扩展的手段取代它的价值包含在
%
或!
变量 - 的
%正常%
扩张发生的只有一次 在执行前行的。这意味着%变量%
扩张具有的相同的值的任何事项,如果该行是在为
命令)。 - 的
!延迟!执行
扩展的每次的该行执行。
- Variable expansion means replace a variable enclosed in
%
or!
by its value. - The
%normal%
expansion happen just once, before a line is executed. This means that a%variable%
expansion have the same value no matters if the line is executed several times (like in afor
command). - The
!delayed!
expansion is performed each time that the line is executed.
请参阅下面的例子:
@echo off
setlocal EnableDelayedExpansion
set "var=Original"
set "var=New" & echo Normal: "%var%", Delayed: "!var!"
输出:
Normal: "Original", Delayed: "New"
还有一句:
@echo off
setlocal EnableDelayedExpansion
set "var1=Normal"
set "var2=Delayed"
for /L %%i in (1,1,10) do (
set "var1=%var1% %%i"
set "var2=!var2! %%i"
)
echo Normal: "%var1%"
echo Delayed: "%var2%"
输出:
Normal: "Normal 10"
Delayed: "Delayed 1 2 3 4 5 6 7 8 9 10"
普通扩张不一定是缺点,而是取决于它被使用的具体情况。例如,在任何其他的编程语言,来交换两个变量的值则需要第三个的帮助,但是在批量它可以在只有一行来完成:
Normal expansion is not necessarily a disadvantage, but depends on the specific situation it is used. For example, in any other programming languages, to exchange the value of two variables you need the aid of a third one, but in Batch it can be done in just one line:
set "var1=%var2%" & set "var2=%var1%"
这篇关于Windows批处理文件:什么是变量扩展,又是什么意思EnableDelayedExpansion?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文