Colby Hall
Colby Hall

Reputation: 3

Batch program closes when trying to us an IF statement

I cannot find the reason why my code closes the program help is needed! When I run the program i can get into the program then it randomly closes I have been trying to fix it for an hour now please help

@echo off
title Services Program
color 0e

set mainMenu=0

goto check_Permissions

:check_Permissions
    echo Administrative permissions required. Detecting permissions...

    net session >nul 2>&1
    if %errorLevel% == 0 (
        echo Success: Administrative permissions confirmed.
    ) else (
        echo Failure: Current permissions inadequate.
    echo Restart this program but run it in administrator mode!
    pause >nul
    )

    pause >nul
    goto main

:main
cls
echo Main Menu
echo.
echo 1 - Start
echo 2 - Options
echo 3 - Exit
set /p id=Enter The Value:%=%

IF %id%==1(
echo Pow
pause
    )
goto main



:program
echo Insert Program here
pause
goto main


:options
echo Options
pause
goto main

Upvotes: 0

Views: 65

Answers (2)

Jhon
Jhon

Reputation: 582

Your if is incorrect

IF %id%==1(

First of all, it needs a space before the parenthesis, then you are making a wrong comparison, since the "==" operator only works when comparing strings and you're trying to compare with a number. What you can do is as follows:

if %id% EQU 1 ( <--- IF only parses numbers when one of the compare-op operators (EQU, NEQ, LSS, LEQ, GTR, GEQ) is used. 
if "%id%" == "1" ( <--- To force a string comparison. The == comparison operator always results in a string comparison.

For a further reading, follow this link

Upvotes: 0

Magoo
Magoo

Reputation: 79983

IF %id%==1(

is incorrect syntax.

IF "%id%"=="1" (

should work. The space before the ( is required

since id may or may not have a value, or may contain spaces or other separators or awkward characters that batch finds distasteful, enclose it in "quotes" to remove the sensitivity to (empty, contains separators) [but not, unfortunately to "unbalanced quotes]

The two sides of the == operator need to exactly match - so the quotes must be included both sides.

Upvotes: 1

Related Questions