奇怪的行为如果

时间:2013-10-09 12:32:11

标签: batch-file cmd

我可能在这里愚蠢,但我根本无法弄清楚这批代码的奇怪行为。

所以我有以下代码:

@echo off

set exitval=0

:selectdir
echo Type the name of the folder where the software is installed (or drag and
echo drop the folder here):

set /p sdir=
set sdir=%sdir:"=%

echo.
if not exist "%sdir%\Lib\Plugins" (
    echo The plugins directory does not exist! Try again ^(y/N^)^?
    set /p c=

    if "%c%" == "y" goto selectdir

    goto exitscript
)

:exitscript
pause
exit /b %exitval%

至少可以说,这并不表示一致的行为:

D:\Development>test.cmd
Type the name of the folder where the software is installed (or drag and
drop the folder here):
C:\xyz

The plugins directory does not exist! Try again (y/N)?
y
Press any key to continue . . .

D:\Development>test.cmd
Type the name of the folder where the software is installed (or drag and
drop the folder here):
C:\xyz

The plugins directory does not exist! Try again (y/N)?
n
Type the name of the folder where the software is installed (or drag and
drop the folder here):
C:\xyz

The plugins directory does not exist! Try again (y/N)?
n
Press any key to continue . . .

上述代码有什么问题?为什么它不能始终如一地工作?

1 个答案:

答案 0 :(得分:2)

我改变了缩进的线条。你需要延迟扩展它的设置方式,这样就不需要了。

@echo off

set exitval=0

:selectdir
echo Type the name of the folder where the software is installed (or drag and
echo drop the folder here):

set /p sdir=
set sdir=%sdir:"=%

echo.
    if exist "%sdir%\Lib\Plugins\" goto :continue
    set "c="
    set /p "c=The plugins directory does not exist! Try again (y/N)? "
    if /i "%c%" == "y" goto :selectdir
    goto :exitscript

    :continue
    echo do more stuff
    goto :eof


:exitscript
pause
exit /b %exitval%