In 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)? 

In vbscript, how do I run a batch file or command, with the environment of the current cmd prompt window?

In vbscript, how do I run a batch file or command, in the current cmd prompt window, without starting a new process. For example. According to script56.chm (the vbscript help apparently) Windows Scri

How do I accept user input on command line for python script instead of prompt

I have python code which asks user input. (e.g. src = input(‘Enter Path to src: ‘). So when I run code through command prompt (e.g. python test.py) prompt appears ‘Enter path to src:’. But I want to t

Windows Command Prompt(cmd) move command

So I just started to learn how to use the Command Prompt in windows (i used the linux one for a long time) and i wanted to move a file from somewhere to an other place and its not working C:/>move

How to do “diff -r” of UNIX in Windows Cmd Prompt?

How do I compare two directories on cmd prompt of Windows m/c? I want equivalent of diff -r of UNIX.

How do I use colour with Windows command prompt using Python?

I’m trying to patch a waf issue, where the Windows command prompt output isn’t coloured when it’s supposed to be. I’m trying to figure out how to actually implement this patch, but I’m having trouble

maven user prompt works from windows cmd, not from cygwin

I recently reinstalled cygwin from fresh, and am now seeing an issue with Maven builds. A specific maven plugin I am attempting to use pops up an interactive prompt… From the windows cmd, the user

user input in R (Rscript and Widows command prompt)

I am trying to figure out, how can i run an r script, using Rscript in windows command prompt and ask for user input. So far, i have found answers on how do ask for user input in R’s interactive shell

Changing default startup directory for command prompt in Windows 7

How do I change default startup directory for command prompt in Windows 7? I usually do the following to start command prompt from C:/ WIN-R (Run Prompt) cmd /K cd C:/ I want to do the following to

How do I prompt for user input in Maven?

I’m writing a Maven plugin which I’d like to prompt for a simple user input and decide whether to halt the plugin’s execution. I’d like to do something like this: $> mvn myplugin:run [MAVEN] would

How can I prompt for input using Selenium/Webdriver and use the result?

I would like to allow for user input and make some decisions based on it. If I do this: driver.execute_script(prompt(‘Enter smth’,’smth’)) I get a nice prompt, but I cannot use it’s value. Is there

Answers

Try this:

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

You can then use %id% as a parameter to another batch file. For example:

jstack %id%

EDIT: This works just fine for me. Sorry I can’t help more.

set /P id=Enter id: %=%
jstack %id% > jstack.txt

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.

The syntax is as such: set /p variable= [string]

Note the space after the equals(=) sign

Check out http://commandwindows.com/batch.htm or http://www.robvanderwoude.com/userinput.php for a more deep dive into user input with the different versions of win OS batch files.

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 %variable name% syntax

Dollar signs around the variable do not work on my Vista machine, but percent signs do. Also note that a trailing space on the “set” line will show up between the prompt and user input.

@echo off :start set /p var1=”Enter first number: ” pause

@echo off
set /p input="Write something, it will be used in the command "echo""
echo %input%
pause

if i get what you want, this works fine. you can use %input% in other commands too.

@echo off
echo Write something, it will be used in the command "echo"
set /p input=""
cls
echo %input%
pause>nul

thi is more polite.

Just added the

set /p NetworkLocation= Enter name for network?

echo %NetworkLocation% >> netlist.txt

sequence to my netsh batch job. It now shows me the location I respond as the point for that sample. I continuously >> the output file so I know now “home”, “work”, “Starbucks”, etc. Looking for clear air, I can eavulate the lowest use channels and whether there are 5 or just all 2.4 MHz WLANs around.

There is no documented /prompt parameter for SETX as there is for SET.

If you need to prompt for an environment variable that will survive reboots, you can use SETX to store it.

A variable created by SETX won’t be usable until you restart the command prompt. Neatly, however, you can SETX a variable that has already been SET, even if it has the same name.

This works for me in Windows 8.1 Pro:

set /p UserInfo= "What is your name?  "
setx UserInfo "%UserInfo%"

(The quotation marks around the existing variable are necessary.)

This procedure allows you to use the temporary SET-created variable during the current session and will allow you to reuse the SETX-created variable upon reboot of the computer or restart of the CMD prompt.

(Edited to format code paragraphs properly.)