Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Template for Yes/No Choice input in batch files
@ECHO OFF
:start
SET choice=
SET /p choice=Do something? [N]:
IF NOT '%choice%'=='' SET choice=%choice:~0,1%
IF '%choice%'=='Y' GOTO yes
IF '%choice%'=='y' GOTO yes
IF '%choice%'=='N' GOTO no
IF '%choice%'=='n' GOTO no
IF '%choice%'=='' GOTO no
ECHO "%choice%" is not valid
ECHO.
GOTO start
:no
ECHO Do all of the no things here!
PAUSE
EXIT
:yes
ECHO Do all of the yes things here!
PAUSE
EXIT
@Himel-Sarkar

This comment has been minimized.

Copy link

@Himel-Sarkar Himel-Sarkar commented Aug 27, 2020

Sir, is there any way that automatically takes yes if the user does nothing?

@DreAdeDcoRpSE

This comment has been minimized.

Copy link

@DreAdeDcoRpSE DreAdeDcoRpSE commented Aug 27, 2020

Sir, is there any way that automatically takes yes if the user does nothing?

Yes you can. Using his example, on line 10, just change no to yes and that should do it.

@jcefoli

This comment has been minimized.

Copy link
Owner Author

@jcefoli jcefoli commented Aug 28, 2020

Sir, is there any way that automatically takes yes if the user does nothing?

Yes you can. Using his example, on line 10, just change no to yes and that should do it.

Changing line 10 from no to yes will not automatically select yes if no input is provided. IF '%choice%'=='' GOTO yes would just skip to the :yes block instead of the no block if Enter was pressed without providing input. Set /p will wait forever for input.

Is there any reason you'd need this? It's rather dated. I would recommend using PowerShell as a much more modern and capable way of scripting.

@DreAdeDcoRpSE

This comment has been minimized.

Copy link

@DreAdeDcoRpSE DreAdeDcoRpSE commented Sep 3, 2020

Changing line 10 from no to yes will not automatically select yes if no input is provided. IF '%choice%'=='' GOTO yes would just skip to the :yes block instead of the no block if Enter was pressed without providing input. Set /p will wait forever for input.

Yes, you are correct. I misread that. I was thinking he meant if they just hit "Enter" without putting in a selection, it would just take them to the "yes" block.

@kybo15

This comment has been minimized.

Copy link

@kybo15 kybo15 commented May 17, 2021

Minor fixes this:

IF '%choice%'=='Y' GOTO yes
IF '%choice%'=='y' GOTO yes

Replace with this:
IF /i '%choice%'=='Y' GOTO yes
It supports uppercase and lowercase text input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment