1

I have the following batch script:

@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

The problem is that if I run it on the first time, the java jar file is throwing exceptions because it doesn't understands the variable arguments %argi% and %argii%. I press [b] to start again at the options menu, I do the same thing again and voila, it works! Why? Why it doesn't work at the first time, and if i press [b] to go back and put again the args it works fine every time?

I start the app: 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!

If I kill the script with Ctrl-C and reopen it, it will have the same behaviour again...first time error, and then success. Thank you!

Andrew
  • 37
  • 4
  • 1
    Possible duplicate of [How to set a variable inside a loop for /F](http://stackoverflow.com/questions/13805187/how-to-set-a-variable-inside-a-loop-for-f) – Ryan Bemrose Jun 10 '16 at 04:42

1 Answers1

0

you need 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
npocmaka
  • 55,367
  • 18
  • 148
  • 187