Click here to Skip to main content
15,867,488 members
Articles / General Programming
Tip/Trick

Get user input from DOS prompt

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
13 Nov 2010CPOL 300.4K   5   7
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)


Written By
India India
Ravikiran Malladi is a senior software professional in Microsoft technologies. He has global IT experience of over 10 years and has compulsive habit of writing tools, particularly for the projects he works on. He has vast experience in the roles of senior developer, technical analyst, technical architect besides in technical lead roles.

His hobbies include chess, music, cricket, web sites along side writing technical articles and technical blogs.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 1477111623-Aug-21 21:27
Member 1477111623-Aug-21 21:27 
QuestionHow do i do this in DOS? Pin
Member 1477111610-May-20 20:56
Member 1477111610-May-20 20:56 
SuggestionThis will not work in DOS Pin
Member 189251819-Dec-19 7:11
Member 189251819-Dec-19 7:11 
QuestionMy Vote of 5 Pin
David A. Gray13-Nov-17 22:12
David A. Gray13-Nov-17 22:12 
GeneralReason for my vote of 5 quick answer, some time when working... Pin
dennislx7-Dec-11 18:44
professionaldennislx7-Dec-11 18:44 
GeneralReason for my vote of 4 Help me, good job! Pin
dennislx7-Sep-11 7:34
professionaldennislx7-Sep-11 7:34 
GeneralJust to add to it... The page &lt;a href="http://ss64.com/nt... Pin
GuruprasadV28-Dec-10 17:35
GuruprasadV28-Dec-10 17:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.