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.
推荐答案
- 变量扩展意味着用它的值替换
%
或!
中的变量. %normal%
扩展只发生一次,在执行一行之前.这意味着%variable%
扩展具有相同的值,无论该行是否执行多次(例如在for
命令中).!delayed!
扩展每次执行该行.- 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"
普通展开不一定是缺点,而是要看具体的使用情况.例如,在任何其他编程语言中,要交换两个变量的值,您需要第三个变量的帮助,但在 Batch 中,只需一行即可完成:
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屋!
查看全文