2013-10-09 44 views
0

我可能在這裏愚蠢的,但我根本無法弄清我有這種批處理代碼的奇怪行爲。奇怪的行爲,如果

所以我有下面這段代碼:

@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 . . . 

什麼是與上面的代碼的問題嗎?爲什麼它不一致?

回答

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%