1

I have a program which has following flow. Problem is the windows batch file doesn't properly checks errorlevel and doesn't set KILLSTS value. Could you please let me know what's wrong with this program and how to fix this?

Ask user to open an exe
if Yes
   check exe is running or not
      if running, ask user whether to close that exe
          if yes close exe
   run the exe
else
  exit 

Here is the sample batch file.

@ECHO OFF
@REM SETLOCAL ENABLEEXTENSIONS
SET /P AREYOUSURE="Open Spring STS [y/n]>"

set AREYOUSURE=%AREYOUSURE:~0,1% 
ECHO AREYOUSURE=%AREYOUSURE:~0,1% 
IF /I %AREYOUSURE% == N (
    SET /A errno^|=%ERROR_OTHERCOMMAND_FAILED%
    echo Existing Batch
    EXIT /B %errno%
)
SETLOCAL
@REM SET KILLSTS=Y
tasklist /fi "IMAGENAME eq STS.exe" |find ":" > nul
ECHO Error %errorlevel%
IF %errorlevel% neq 0 (
SETLOCAL
SET /P KILLSTS="Spring STS is running. Kill STS Process [y/n]>"
echo KILLSTS %KILLSTS%
set KILLSTS=%KILLSTS:~0,1% 
echo KILLSTS AFTER SUBSTR %KILLSTS%
IF /I %KILLSTS% == Y TASKKILL /f /im "STS.exe"

ENDLOCAL
)

START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"

I am getting below error enter image description here

Debopam
  • 3,198
  • 6
  • 41
  • 72
  • 3
    Possible duplicate of [Windows Batch Variables Won't Set](http://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set) – SomethingDark Mar 02 '17 at 18:10

2 Answers2

1

You need to learn how to properly format if statements.

You are formatting them as:

IF /I %KILLSTS% == Y TASKKILL /f /im "STS.exe"

When they should be formatted as:

if /i "%KILLSTS%"=="Y" (TASKKILL /f /im STS.exe)

The formatting doesn't really matter as such in simple batch files, but it's best to use the correct syntax which can handle special characters such as SPACES, AMPERSANDS, QUOTES, PIPE for when more complex variables are involved.


Updated script:

@ECHO OFF
@REM SETLOCAL ENABLEEXTENSIONS
    SET /P "AREYOUSURE=Open Spring STS [y/n]>"

    set "AREYOUSURE=%AREYOUSURE:~0,1% "
    echo "AREYOUSURE=%AREYOUSURE:~0,1%"
IF /I "%AREYOUSURE%"=="N" (
    SET /A errno^|=%ERROR_OTHERCOMMAND_FAILED%
    echo Existing Batch
    EXIT /B %errno%
)
    SETLOCAL
    @REM SET KILLSTS=Y
    tasklist /fi "IMAGENAME eq STS.exe" | find ":" > nul
    ECHO Error %errorlevel%
IF "%errorlevel%" neq "0" (
    call :escapeexpansion
)
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"
exit /b
:escapeexpansion
    SETLOCAL
    SET /P "KILLSTS=Spring STS is running. Kill STS Process [y/n]>"
    echo KILLSTS %KILLSTS%
    set "KILLSTS=%KILLSTS:~0,1%"
    echo KILLSTS AFTER SUBSTR %KILLSTS%
    IF /I "%KILLSTS%"=="Y" TASKKILL /f /im "STS.exe"
    ENDLOCAL
goto :EOF
Sam Denty
  • 3,693
  • 3
  • 30
  • 43
0

The entire structure seems wrong to me; as well as pointlessly using SET /P instead of CHOICE.

@ECHO OFF
TASKLIST /FI "IMAGENAME eq STS.exe"|FIND ":">NUL 2>&1&&GOTO ASKIF
CHOICE /M "Spring STS is running. Kill STS Process"
IF ERRORLEVEL 2 GOTO ENDIT
TASKKILL /F /IM "STS.exe"
TIMEOUT 3 /NOBREAK>NUL

:ASKIF
CHOICE /M "Open Spring STS"
IF ERRORLEVEL 2 GOTO ENDIT
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"

:ENDIT
Echo=Exiting Batch
TIMEOUT 3 /NOBREAK>NUL
Compo
  • 36,585
  • 5
  • 27
  • 39
  • thanks for the solution. But could you please explain the line TASKLIST /FI "IMAGENAME eq STS.exe"|FIND ":">NUL 2>&1&&GOTO ASKIF – Debopam Mar 06 '17 at 22:00
  • You don't need the result of `TASKLIST`, just whether it was successful. The potential output, both StdOut, `1>`, and StdErr, `2>`, are both sent to a Nul device, `>NUL 2>&1`, for that reason. `&&` effectively means, 'if the previous command was successful then'; (the opposite being `||`). In summary, if the `FIND` command is successful then `GOTO ASKIF` – Compo Mar 06 '17 at 23:08
  • Thanks for the explanation – Debopam Mar 07 '17 at 00:46