这真的是扩展问题吗?

问题描述 投票:0回答:1

在批处理文件中,这有效:

@echo off
:start
set /p field3=
cls
echo checking field 3
goto %field3%

:a
echo field3 is a
goto start

:b
echo field3 is b
goto start

:c
echo field 3 is c
goto start

然而这并不:

@echo off
:start
set /p field3=
cls
echo checking field 3
if "%field3%"=="" goto start else goto %field3%
REM goto %field3%

:a
echo field3 is a
goto start

:b
echo field3 is b
goto start

:c
echo field 3 is c
goto start

在最后一个示例中,field3 保持不变并且不会改变。据我了解,如果没有延迟扩展,field3将保持不变,在“if”语句中解析该行时不会改变。但为什么它可以与

goto %field3%
一起使用?

batch-processing
1个回答
0
投票

我相信我发现了我的错误。在“If”语句中,至少在批处理文件中,以下命令需要分开,否则它们将被作为一个整体读取。所以

if "%field3%"=="" goto start else goto %field3%
需要是
if "%field3%"=="" (goto start) else (goto %field3%)
。这在各处都有详细记录,但我只是没能抓住它。

© www.soinside.com 2019 - 2024. All rights reserved.