本文介绍了Windows批处理内设定:如果不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此脚本(从.bat文件):

when I'm running this script (from a .bat file):

set var1=true
if "%var1%"=="true" (
  set var2=myvalue
  echo %var2%
)

我总是得到:

ECHO is on.

含义 VAR2 变量是没有真正建立。
任何人都可以请帮助我理解了为什么?

Meaning the var2 variable was not really set.Can anyone please help me understand why?

推荐答案

VAR2设置,但在该行扩张回声%VAR2%发生之前执行该程序块。结果
这时 VAR2 是空的。

var2 is set, but the expansion in the line echo %var2% occurs before the block is executed.
At this time var2 is empty.

因此​​,delayedExpansion存在语法错误,它使用而不是和它在执行时计算,不解析时间。

Therefore the delayedExpansion syntax exists, it uses ! instead of % and it is evaluated at execution time, not parse time.

setlocal EnableDelayedExpansion
set var1=true
if "%var1%"=="true" (
  set var2=myvalue
  echo !var2!
)

这篇关于Windows批处理内设定:如果不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:00