Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Get user input from DOS prompt

, 13 Nov 2010
Rate this:
Please Sign up or sign in to vote.
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)

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)

Share

About the Author

mravikiran

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

 
GeneralReason for my vote of 5 quick answer, some time when working... Pinmemberdennislx7-Dec-11 18:44 
GeneralReason for my vote of 4 Help me, good job! Pinmemberdennislx7-Sep-11 7:34 
GeneralJust to add to it... The page &lt;a href="http://ss64.com/nt... PinmemberGuruprasadV28-Dec-10 17:35 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 13 Nov 2010
Article Copyright 2010 by mravikiran
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid