Batch Script SET /p without a value

16,436

Solution 1

You can initialize choice to some invalid value before set /p, e.g.:

SET choice=none

To print the appropriate error message, you can do

IF %choice% EQU none (
   (ECHO You did not select an option.) & PAUSE & GOTO:mainmenu
)

Solution 2

Set /p doesn't change the content of a variable, if the user doesn't enter text.
So you can simply set your variable to nothing to detect if the user enter anything.
Set "choice="

Or you can use the errorlevel, as it is set to 1 if the input is empty
But be careful, as it doesn't reset it to 0. So you have to force it yourself before.

cd.
Set /p choose=
If %errorlevel%==1 goto empty

And you should use the variables with delayed expansion, as it is always safe. Else a user can break your script with input like "&exit"

Share:
16,436
Anthony Miller
Author by

Anthony Miller

"Your competition is a hungry immigrant with a digital handheld assistant. America is made up of immigrants... if your children are to be successful they must act like immigrants in their own country. Just by being born here doesn't give you the ability to be successful... it is the work ethic... the pioneering ethic... the service ethic that will win. Your competition is always a hungry immigrant with a digital assistant: hungry in the belly for food, hungry in the mind for knowledge, and the hunger is something that should never leave you." ~Dr. Dennis Waitley

Updated on July 12, 2022

Comments

  • Anthony Miller
    Anthony Miller almost 2 years

    Creating a batch script in Windows XP. Here's a snippet of code I'm having problems with:

    ::==============================================================::
    ::How many scripts are included in this program?
    SET NumOfScripts=4
    ::==============================================================::
    
    :mainmenu
    CLS
    ECHO [MAIN MENU]
    ECHO Please choose from the following options to run a particular script:
    set /p choice="[1] SCRIPT1.bat | [2] SCRIPT2.bat | [3] SCRIPT3.bat | [4] SCRIPT4.bat  :  "
    
    IF %choice% EQU 1 CALL :SCRIPT1
    IF %choice% EQU 2 CALL :SCRIPT2
    IF %choice% EQU 3 CALL :SCRIPT3
    IF %choice% EQU 4 CALL :SCRIPT4
    
    REM Wrong Choices
    IF %choice% GTR %NumOfScripts% (
     (ECHO You have entered an invalid option. Please press any key to be taken back to the main menu.) & PAUSE & GOTO:mainmenu
    )
    IF %choice% LEQ 0 (
     (ECHO You have entered an invalid option. Please press any key to be taken back to the main menu.) & PAUSE & GOTO:mainmenu
    )
    ECHO You have entered an invalid option. Please press any key to be taken back to the main menu 
    PAUSE & GOTO:mainmenu
    

    Looking under REM Wrong Choice, the first two arguments work as they should, however, if the user enters in no value (just presses the enter key) it automatically terminates the script. I've added IF NOT DEFINED choice and that doesn't work... I also tried IF [%choice%]==[] and IF [%choice%] EQU [] and those don't work either.

    Here's the funny thing... you enter an invalid digit, say 5 or -1, it will give the echoes and go back to the main menu as it should... THEN if you just press enter without a value inserted, it will echo and go back to the main menu as it should.

    My question is how do you get it to recognize that the user did not enter a value for set /p on the first go?