[SOLVED] Script closes when I press enter using set /p.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 113
Joined: 12 Aug 2019 13:57

[SOLVED] Script closes when I press enter using set /p.

#1 Post by PAB » 16 Jul 2020 18:06

Good evening,

I hope everybody is OK and keeping SAFE!

I have a batch script that works well, except, if I DON'T enter a number and press <ENTER>, it exits the script. Here is the bit of code that I think is affecting it . . .

Code: Select all

setlocal EnableDelayedExpansion

set "userinput="
set /p "userinput=>Enter a Menu number and press <Enter>: "

if /i %userinput%==0  goto Exit_Program
if /i %userinput%==1  goto Standard
if /i %userinput%==2  goto Advanced
I need the setlocal EnableDelayedExpansion for other parts of the code which all work OK!

In fact, it is only the set /p that is giving me a problem!

I have a couple of scripts in the same format except they use Choice instead, and when I press <ENTER> instead of entering a number it does NOT close the batch file but waits for a valid number input.

I have tried . . .

Code: Select all

if /i '%userinput%'=='0'  goto Exit_Program
if /i '%userinput%'=='2'  goto Standard
if /i '%userinput%'=='3'  goto Standard
. . . among many other things.

Thanks in advance.

EDIT 1:

Thanks to Google, I have found a solution, although I don't know quite why it works . . .

Code: Select all

if /i [%userinput%]==[] cls & goto :Menu
Can anybody explain what the [] brackets do in the line of code please because I always use speech marks normally?

Thanks in advance.

EDIT 2:

OK, this works sort of!

Code: Select all

if /i "%userinput%"=="" cls & goto :Menu
if /i "%userinput%"=="0"  goto Exit_Program
if /i "%userinput%"=="1"  goto Standard
if /i "%userinput%"=="2"  goto Advanced
if /i "%userinput%"=="R"  goto Reports_Menu
If I enter a letter and press <ENTER>, the batch script closes.
There is one letter than is included in the menu [ see above ], the letter "R", which when I enter it and press <ENTER> takes me to the correct place.

Has anyone got any ideas please?

Thanks in advance.
Last edited by PAB on 19 Jul 2020 15:53, edited 1 time in total.

Squashman
Expert
Posts: 4180
Joined: 23 Dec 2011 13:59

Re: Script closes when I press enter using set /p.

#2 Post by Squashman » 16 Jul 2020 21:58

By pressing enter, the variable is undefined. So when you are not using quotes the execution of the command becomes:

Code: Select all

if /i ==0  goto Exit_Program
That is invalid syntax.

The safe way to compare strings as you found out is to use quotes.

PAB
Posts: 113
Joined: 12 Aug 2019 13:57

Re: Script closes when I press enter using set /p.

#3 Post by PAB » 17 Jul 2020 01:41

Squashman wrote:
16 Jul 2020 21:58
By pressing enter, the variable is undefined. So when you are not using quotes the execution of the command becomes:

Code: Select all

if /i ==0  goto Exit_Program
That is invalid syntax.

The safe way to compare strings as you found out is to use quotes.
Thank you Squashman, it is appreciated.

I also solved that if letters other than "R" were entered and you pressed <ENTER> it would exit by using . . .

Code: Select all

if /i "%userinput%" NEQ "R" cls & goto :Menu
Thanks again.

Post Reply