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

1

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

thanks for the solution. But could you please explain the line TASKLIST /FI "IMAGENAME eq STS.exe"|FIND ":">NUL 2>&1&&GOTO ASKIF
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
Thanks for the explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.