Home > Enterprise >  Checking for a specific letter in a BAT file [closed]
Checking for a specific letter in a BAT file [closed]

Time:09-29

I'm simply looking to find the correct syntax for having a BAT file check for a specific letter, an if that letter is not a valid selection, to go to an error. My first three lines of code work, but the statements checking if the letter entered is NOT a C or an R simply makes the BAT file exit, instead of going to my error.

IF /I "%input%"=="C" goto compile
IF /I "%input%"=="R" goto run
IF /I "%input%"=="" goto error
IF /I "%input%"!="C" goto error
IF /I "%input%"!="R" goto error

What is the correct Syntax please for the last two lines of code?

CodePudding user response:

As another user suggested, look at CHOICE https://ss64.com/nt/choice.html

To answer your direct questions:

you need to use the NOT operator

IF /I NOT "%input%"=="C" goto error

OR

IF /I "%input%" NEQ "C" goto error

All I think you need to do is simply replace you last 2 lines with your error handling and then goto end That way if you get to this point it's an error. Add a goto end before your :compile and :run labels to prevent wrongful execution.

IF /I "%input%"=="C" goto compile
IF /I "%input%"=="R" goto run
:error
//handle your error...
goto end
:compile
//handle compile
goto end
:run
//handle run
:end
//handle end

For reference:

https://ss64.com/nt/if.html

  • Related