2016-06-10 68 views
1

我有以下批处理脚本:批次变量不能

@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]返回并再次将参数设置为每次都正常工作?

我启动的应用程序: 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

的可能的复制[如何设置/F](http://stackoverflow.com/questions/13805187/how-to-set-a循环中有一个变量-variable-inside-a-loop-for-f) –

回答

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 
+0

谢谢!它像一个魅力一样工作! – Andrew