批处理变量不起作用

时间:2016-06-10 03:12:55

标签: batch-file

我有以下批处理脚本:

@echo off
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service                   Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
    echo Give the parameters:
    set /p argi=
    set /p argii=
    START /B java -jar JavaClient.jar %argi% %argii%
)

if %udefine%==e goto exit

cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit

问题在于,如果我第一次运行它,java jar文件会抛出异常,因为它不了解变量参数%argi%和%argii%。我按[b]再次在选项菜单中重新开始,我再次做同样的事情,瞧,它有效!为什么?为什么它第一次没有工作,如果我按[b]返回并再次放置args它每次都能正常工作?

我启动应用: Here I enter 1, then give the 2 variables argi and argii Press Enter, and the jar throws an error because it doesn't 'understands the arguments Then I press b to go back and take it all over again And voila! The result as expected!

如果我使用Ctrl-C终止脚本并重新打开它,它将再次具有相同的行为...第一次出错,然后成功。 谢谢!

1 个答案:

答案 0 :(得分:0)

您需要delayed expansion

@echo off
setlocal enableDelayedExpansion
color 2f
title "USERS MANAGEMENT IN A LAN"
:top
echo ***************************************************************
echo.
echo SELECT ACTIONS
echo.
echo ***************************************************************
echo.
echo [1] Ping the service                   Params: unu
echo [2] TBD
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 (
    echo Give the parameters:
    set /p argi=
    set /p argii=
    START /B java -jar JavaClient.jar !argi! !argii!
)

if %udefine%==e goto exit

cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another option.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using it
echo.
echo ***************************************************************
pause
exit