Bharani
Bharani

Reputation: 303

Interactive Batch File

How to proceed with an interactive batch file?

Eg.,

DO you want to continue? [y/n]

If 'y' Goto Label1

Else Goto Label2

Thanks

Upvotes: 2

Views: 16015

Answers (5)

J. Bond
J. Bond

Reputation: 504

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.

Upvotes: 1

user3093687
user3093687

Reputation: 35

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.

Upvotes: 0

Dante J. Dummer
Dante J. Dummer

Reputation: 1

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

Upvotes: 0

EggyBach
EggyBach

Reputation: 4228

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

Upvotes: 6

Timbo
Timbo

Reputation: 28050

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

Upvotes: 4

Related Questions