Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming

Get user input from DOS prompt

5.00/5 (14 votes)
13 Nov 2010CPOL 304K  
Command Line Inputs

Introduction


A lot of times, when we write batch files (*.bat) or command files (*.cmd), we would want to take user input at the DOS prompt during runtime of the script. This article explains how to fulfill that need.



Taking User Input at DOS Prompt


It is a very simple way to get the user input at the DOS prompt. You need to use the SET command with a variable name for this purpose.



SET Command (syntax)


C++
SET /P <var>=[<prompt message>]


Example


@ECHO OFF
SET /P uname=Please enter your name: 
IF "%uname%"=="" GOTO Error
ECHO Hello %uname%, Welcome to DOS inputs!
GOTO End
:Error
ECHO You did not enter your name! Bye bye!!
:End


Points of Interest


The word "uname" in the above script is the variable that captures the user input. The above script also shows the validation whether any input is given or not. Use GOTO and Labels to control the flow.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)