Not satisfied by the Answer? Still have doubts?
Ask Question

Windows cmd, how do I prompt for user input and use the result in another command?

I have a Windows .bat file which I would like to accept user input and then use the results of that input as part of the call to additional commands.

For example, I'd like to accept a process ID from the user, and then run jstack against that ID, putting the results of the jstack call into a file. However, when I try this, it doesn't work.

Here's my sample bat file contents:
@echo off
set /p id=Enter ID:
echo %id%
jstack > jstack.txt


and here's what shows up in jstack.txt:
Enter ID: Terminate batch job (Y/N)? 
prompt batch-file windows
by

3 Answers

akshay1995
Try this:

@echo off
set /p id="Enter ID: "

You can then use %id% as a parameter to another batch file like jstack %id%.

For example:

set /P id=Enter id:
jstack %id% > jstack.txt
sandhya6gczb
The syntax is as such:
set /p variable=[string]

Once you have set your variable, you can then go about using it in the following fashion.

@echo off
set /p UserInputPath=What Directory would you like?
cd C:%UserInputPath%

note the %VariableName% syntax
kshitijrana14
I am not sure if this is the case for all versions of Windows, however on the XP machine I have, I need to use the following:
set /p Var1="Prompt String"

Without the prompt string in quotes, I get various results depending on the text.

Login / Signup to Answer the Question.

  • Be descriptive with your answer, and try to avoid copy-pasting external links, instead guide the user who has asked the question, where can they find the detailed answer.
  • You cannot share any URL in answer.
  • To make a text bold, surround the text with single asterisk, for example *bold text*
  • To format a text like code, surround the text within double asterisk, for example **code**
  • To add a multiline piece of code, surround the whole code within triple asterisk, for example *** multiline code ***
  • Please verify before submitting the answer.