Question

How to proceed with an interactive batch file?

Eg.,

DO you want to continue? [y/n]

If 'y' Goto Label1

Else Goto Label2

Thanks

Was it helpful?

Solution

You can use the SET command. The following is the DOS command equivalent of the pseudo code you have above:

set /p choice=Do you want to continue? [y/n]
if '%choice%'=='Y' goto label1
goto label2

OTHER TIPS

Using the choice command, you can specify a set of valid characters and a message:

choice /C YN /M "Do you want to continue?"
if errorlevel 2 goto labelno
if errorlevel 1 goto labelyes

set /p choice= Do you want to continue? [y/n] if "%choice%" == "y" ( goto label1 ) else goto label2

Most of your guys' answers i like but the most I would do is (for example this code is my way of organization.

:Label1
@echo off
color (any color)
title (anything here)

set /p choice=Do you want to continue? [y/n]
IF %choice% == y goto Label2
IF %choice% == n exit

:Label2
cls
echo you chose Yes.

I know 1 way to do this (like @user3093687 posted)

set /p choice=Do you want to continue? (y,n)
if /I %choice%==y goto Label2
if /I %choice%==n goto exit

:Label2
Cls
echo You chose yes

:exit
exit

The /I in the second and third line of the batch file makes it so that it is not case sensitive and special characters are ignored (example Y and y will be both read as y.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top