0

I have this weird thing going on that I can't figure out. In my batch script I'm running a java cli program that connects to a server which I have no problems doing. But in order to run the program I have the user type in their server credentials. If the login fails, I get an "Error" string as a response from the java program which I then ask the user if they want to try again.

After checking the response for "Error", my problem occurs when asking the user if they want to try again. For whatever reason nothing get captured/stored in my tryAgain variable.

for /f %%i in ('java -jar cli-launcher.jar -a https://127.0.0.1:8000 -u admin -p admin -s someOtherScript.txt) do set response=%%i

::Something about this IF statement is causing an issue
IF NOT %response::=%==Error (echo "No Error"
) else (SET /p tryAgain=Try Again: 
  echo "%tryAgain%"
)

EDIT:

Looks like my problem has to with my checking of the response string. When I pull out the tryAgain user capture, it works fine. Can anyone see what I'm doing wrong?

for /f %%i in ('java -jar cli-launcher.jar -a https://127.0.0.1:8000 -u admin -p admin -s someOtherScript.txt) do set response=%%i

::This works fine
SET /p tryAgain=Try Again: 
echo "%tryAgain%"
Mofi
  • 46,139
  • 17
  • 80
  • 143
jnasty
  • 79
  • 1
  • 2
  • 9
  • I'm in a hurry, but to be short: search for "delayed expansion" or "setlocal enabledelayedexpansion" Also some help and examples is available in `for /?` – Stephan Aug 05 '14 at 16:55

1 Answers1

1

You can use following:

@echo off
setlocal EnableDelayedExpansion
for /f %%i in ('java -jar cli-launcher.jar -a https://127.0.0.1:8000 -u admin -p admin -s someOtherScript.txt) do set response=%%i

if not "!response::=!"=="Error" (
   echo "No Error"
) else (
   Set /p "tryAgain=Try Again (y/n)? " 
   echo "!tryAgain!"
)
endlocal

What setlocal EnableDelayedExpansion does and why ! is used instead of % on referencing the environment variables is explained in help of command set output in a command prompt window after entering either set /? or help set.

Take also a look on answer on IF statement in batch causing issues. Works when ran individually? written by MC ND.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143