How do I read input from the console in a batch file? What I am trying to achieve is the functionality of scanf in C. How can I do the same in a batch file?
Asked
Active
Viewed 2.5e+01k times
103
-
possible duplicate of [Problem with user input in my batch file](http://stackoverflow.com/questions/916413/problem-with-user-input-in-my-batch-file) – Raymond Chen Oct 24 '11 at 18:09
-
@ Raymond : Nope, question is to read multiple characters as a string from keyboard into a variable. – Nohsib Oct 24 '11 at 18:16
-
The command `set /p` reads whatever the user types, whether it be one letter or multiple. – Raymond Chen Oct 24 '11 at 18:39
3 Answers
169
The code snippet in the linked proposed duplicate reads user input.
ECHO A current build of Test Harness exists.
set /p delBuild=Delete preexisting build [y/n]?:
The user can type as many letters as they want, and it will go into the delBuild variable.

Raymond Chen
- 44,448
- 11
- 96
- 135
-
42
-
4Have to notice that `echo !delBuild!` will give you a more reliably output on execution when `setlocal EnableDelayedExpansion` enabled – xacinay Jun 17 '14 at 13:52
36
If you're just quickly looking to keep a cmd instance open instead of exiting immediately, simply doing the following is enough
set /p asd="Hit enter to continue"
at the end of your script and it'll keep the window open.
Note that this'll set asd
as an environment variable, and can be replaced with anything else.

TankorSmash
- 12,186
- 6
- 68
- 106
-
21for this purpose, someone invented the `pause` command. Also it's `set /p`, not `set \p`. – Stephan Mar 20 '15 at 06:07
-
6"As everyone is saying", implies that the answer is already given, so another one without more informatiion is needless – jeb Mar 20 '15 at 06:21
-
1@jeb you're right. my issue was that they included variables and echo, while I didn't need any of that. It took me a minute or two to parse their answers, as I've got no batch experience. I believe my answer satisfies the bare minimum answer suited for my needs. – TankorSmash Mar 20 '15 at 15:41
-
@Stephan thanks dude, on both counts. I was looking for a way to keep the cmd instance for exiting after a script had completed, simply pausing for X time wasn't enough. – TankorSmash Mar 20 '15 at 15:42
-
@ArslanAhmad I just tested, it still works from `cmd`, but not Powershell. Make sure you're using the right thing. – TankorSmash Apr 20 '19 at 17:37
-
For anyone testing this example, have in mind that the `temp` environment setting usually points to the temporary folder, so doing this **overwrites** your setting. Fortunately, if you happen to run this in a cmd window, it only affects that instance and after running `exit` the damage is lost. You can check your current value by executing `set temp`. – Andrew Jan 08 '20 at 11:09
23
In addition to the existing answer it is possible to set a default option as follows:
echo off
ECHO A current build of Test Harness exists.
set delBuild=n
set /p delBuild=Delete preexisting build [y/n] (default - %delBuild%)?:
This allows users to simply hit "Enter" if they want to enter the default.

steviethecat
- 870
- 9
- 14

Richard Williams
- 2,044
- 1
- 24
- 28