Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file Windows批处理文件-启用延迟扩展查询_Batch File_Delayedvariableexpansion - Fatal编程技术网

Batch file Windows批处理文件-启用延迟扩展查询

Batch file Windows批处理文件-启用延迟扩展查询,batch-file,delayedvariableexpansion,Batch File,Delayedvariableexpansion,在网上阅读了一篇已有的文章,并在网上进行了一些阅读。在我掉了太多头发之前,我想是时候发布我的问题了 在Windows XP SP3下双击以运行的批处理文件中有以下代码: SETLOCAL ENABLEDELAYEDEXPANSION ::Observe variable is not defined SET test ::Define initial value SET test = "Two" ::Observe initial value is set SET test ::Veri

在网上阅读了一篇已有的文章,并在网上进行了一些阅读。在我掉了太多头发之前,我想是时候发布我的问题了

在Windows XP SP3下双击以运行的批处理文件中有以下代码:

SETLOCAL ENABLEDELAYEDEXPANSION

::Observe variable is not defined
SET test

::Define initial value
SET test = "Two"

::Observe initial value is set
SET test

::Verify if the contents of the variable matches our condition
If "!test!" == "Two" GOTO TWO

::First Place holder
:ONE

::Echo first response
ECHO "One"

::Second Place holder
:TWO

::Echo second response
ECHO "Two"

::Await user input
PAUSE

ENDLOCAL
基本上,我试图确定是否可以使用条件在脚本中导航。很明显,我在变量范围和延迟变量扩展方面遇到了一些问题,但我对我做错的事情有点迷茫


有人能给我指出正确的方向吗?

您当前的问题是,您正在将变量设置为“2”的值,您可以在这里看到:

@echo off

SETLOCAL ENABLEDELAYEDEXPANSION

::Observe variable is not defined
SET test

::Define initial value
SET test = "Two"

::Observe initial value is set
SET test
echo %test%
echo..%test %.

::Verify if the contents of the variable matches our condition
If "!test!" == "Two" GOTO TWO

::First Place holder
:ONE

::Echo first response
ECHO "One"

::Second Place holder
:TWO

::Echo second response
ECHO "Two"

::Await user input
PAUSE

ENDLOCAL
产生:

Environment variable test not defined
test = "Two"
. "Two".
"One"
"Two"
Press any key to continue . . .
“set test”输出变量的原因与“set t”输出变量的原因相同——如果没有特定名称的变量,它将输出以该名称开头的所有变量

set命令也是一个挑剔的小野兽,不喜欢“=”字符周围的空格;它将它们(顺便加上引号)合并到环境变量名和分配给它的值中。相反,请使用:

set test=Two
另外,在您使用延迟扩展的地方,这并不重要,因为%test%和!测试!也会扩大同样的范围。它在以下语句中很有用:

if "!test!" == "Two" (
    set test=TwoAndABit
    echo !test!
)
内部echo将输出两个ANDABIT,而当遇到整个if语句时扩展的%test%将导致它输出两个ANDABIT


尽管如此,我总是在任何地方使用延迟扩展,只是为了保持一致性。

SET命令会将等号之后的所有内容都保留到最后一个非空字符中。你的命令

SET test = "Two"
…正在将变量test设置为值“2”,并带有前导空格和引号,而不仅仅是字符串2

所以当你测试的时候

If "!test!" == "Two" GOTO TWO
你真的在测试

If " "Two"" == "Two" GOTO TWO

谢谢你清晰的回应,帕克斯!所有人都阅读、测试并解决了我的问题:)自从糟糕的旧MS-DOS command.com时代以来,它已经走过了很长的一段路——它仍然不是ksh或bash,但它是可行的。