Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Can a batch file prompt for input?

You are able to prompt a user for input using a Batch script function.

How do you input in Command Prompt?

To have a batch file stop and prompt for input, you would want to look into using set /p , which will prompt for input and then assign that input to a variable. e.g.: set /P name="What Is Your Name? " echo Hello %name%!

What does @echo off mean in cmd?

echo off. When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: @echo off.


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

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

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 Windows 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 %VariableName% syntax


set /p choice= "Please Select one of the above options :" 
echo '%choice%'

The space after = is very important.


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.


@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