504

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)? 
6
  • 3
    "Terminate batch job (Y/N)?" typically shows up when you hit ctrl-c while your .bat is running.
    – Jim Buck
    Aug 3, 2009 at 19:54
  • 2
    Jstack.txt will contain the output from jstack and nothing relating to your batch file and also you haven't said what Jstack is.
    – sgmoore
    Jul 30, 2012 at 13:09
  • 1
    Didn't any of these answers work? If not, did you find a solution? If you did, it would be helpful if you posted it here as a self-answer.
    – Maslor
    Sep 1, 2015 at 13:43
  • 1
    If you want fixed options for the input you're better off using choice than set /p
    – icc97
    Dec 30, 2016 at 0:24
  • 2
    Please mark my post as the answer, the essence of which is that when running the batch program you hit Enter instead of supplying an ID at the prompt, and therefore the %id% was set to Enter ID: rather than some ID you were supposed to supply from the keyboard.
    – pashute
    Mar 14, 2018 at 19:21

14 Answers 14

597

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
7
  • 4
    Is there a way to pipe the output of the SET command to NIL:, so you can hide the typing? Like if you wanted to do a: set /p id=Enter ID:\r\n set /p pw=Enter Passwd: and didn't want the password text to show in the window, let alone, show in the "DosKey" command buffer? I've tried several different ways, and haven't been able to find a way to do this, outside of writing a little C program to take the input, and assign it to an EnvVar, but that's cheating! :) I want it to work with JUST Windows CMD's.
    – LarryF
    May 9, 2016 at 17:45
  • 5
    Note the added nicety of the space at the end of "Enter ID: ". Gives a sweet little space between the prompt and the user answer. Delete the internet?No silly vs Delete the internet? No silly.
    – icc97
    Dec 29, 2016 at 23:54
  • No need for the quotes. What @Instantsoup was missing was the '%id%` in the line jstack %id% > jstack.txt
    – pashute
    Mar 14, 2018 at 18:43
  • 2
    Use SETLOCAL and ENDLOCALif you want the keep the variable local to the current batch. See ss64.com/nt/endlocal.html
    – endo64
    Jul 18, 2018 at 14:14
123

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

5
  • Please finish your sentence...are you going to include another link? Also, I tested all of the above in a Windows 7 cmd.exe shell, and each--without exception--assigns user input to an arbitrarily named variable. How is your response embellishing upon what's already been provided?
    – chb
    Jul 28, 2012 at 11:07
  • 3
    When I posted my answer it didn't really look like the original question had been answered. The original question is "How do you ask for user input and then turnaround and use it in another command?" I beleve my answer succintly answered the question. Plus when you search in Google for "how do you get user input from a batch file" this question is #2 in results so it needs the correct answer which I though that this forum was for. Jul 30, 2012 at 12:59
  • 14
    The space after the = is not necessary.
    – Joey
    Jul 31, 2012 at 15:56
  • Why do we need the space? What exactly goes on in the cmd that does not allow space?
    – rassa45
    Aug 4, 2015 at 9:43
  • @Joey I agree, the robvanderderwoude.com page has no space SET /P variable=[promptString]
    – icc97
    Dec 29, 2016 at 23:31
44
set /p choice= "Please Select one of the above options :" 
echo '%choice%'

The space after = is very important.

0
35

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.

3
  • +1 for "various results depending on the text" : I had the same issue. Now it prints the quotes with the text, but there is no more strange behavior.
    – Benj
    Jun 10, 2015 at 7:09
  • 8
    use this syntax to have the quotes, but avoid showing them: set /p "var1=Prompt String"
    – Stephan
    Sep 26, 2016 at 19:33
  • It's useful to use the quotes and include a space at the end as it makes the prompt on the command line nicer. Having the quotes makes it obvious that there is a space there in the batch file. set /p Var1="Prompt String " Then when you run the file and the user types something you get Prompt String Yes instead of Prompt StringYes
    – icc97
    Dec 29, 2016 at 23:50
15
@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 
9

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.)

9

You can try also with userInput.bat which uses the html input element.

enter image description here

This will assign the input to the value jstackId:

call userInput.bat jstackId
echo %jstackId%

This will just print the input value which eventually you can capture with FOR /F :

call userInput.bat
1
  • 2
    Excellent solution. Thank you! Feb 17, 2021 at 13:54
8
@echo off
:start
set /p var1="Enter first number: "
pause
2
  • 3
    Why do you start with :start ?
    – Benj
    Jun 10, 2015 at 7:08
  • 1
    :start is just a marker... For example, he might use a :end as well, that causes things like variable cleanup to happen, or to call a popd, or something like that. Then, you also have the option of using an IF ERRORLEVEL1 GOTO END... or even a GOTO ERR, and under :err have something like echo An Error occured while attempting to get the file number. goto:end It's probably a mater of habit...
    – LarryF
    Apr 30, 2016 at 1:19
6

There are two possibilities.

  1. You forgot to put the %id% in the jstack call.

    jstack %id% > jstack.txt
    

So the whole correct batch file should be:

@echo off
set /p id=Enter ID: 
echo %id%
jstack %id% > jstack.txt

And/Or 2. You did put it in the code (and forgot to tell us in the question) but when you ran the batch file you hit the Enter key instead of typing an ID (say 1234).

What's happening is the result of these two mistakes: jstack is supposed to be called with the id that you supply it.

But in your case (according to the code you supplied in the question) you called it without any variable. You wrote:

jstack > jstack.txt

So when you run jstack with no variable it outputs the following:

Terminate batch file Y/N? 

Your second mistake is that you pressed Enter instead of giving a value when the program asked you: Enter ID:. If you would have put in an ID at this point, say 1234, the %id% variable would become that value, in our case 1234. But you did NOT supply a value and instead pressed Enter. When you don't give the variable any value, and if that variable was not set to anything else before, then the variable %id% is set to the prompt of the set command!! So now %id% is set to Enter ID: which was echoed on your screen as requested in the batch file BEFORE you called the jstack.

But I suspect you DID have the jstack %id% > jstack.txt in your batch file code with the %id (and omitted it by mistake from the question), and that you hit enter without typing in an id. The batch program then echoed the id, which is now "Enter ID:", and then ran jstack Enter ID: > jstack.txt

Jstack itself echoed the input, encountered a mistake and asked to terminate.
And all this was written into the jstack.txt file.

5

I have a little cmd I use when preparing pc to clients: it calls the user for input, and the rename the pc to that.

@ECHO "remember to run this as admin."
@ECHO OFF
SET /P _inputname= Please enter an computername:
@ECHO Du intastede "%_inputname%"
@ECHO "The pc will restart after this"
pause
@ECHO OFF
wmic computersystem where name="%COMPUTERNAME%" call rename name="%_inputname%"

shutdown -r -f
1
  • Hi again this usefull script is used on Win10 pc after initial setup :-)
    – Werdna
    Feb 9, 2018 at 14:59
3

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.

3
  • trailing space? win7 x64 rus: C:\>set /p DestPath="Type path:" and next line is Type path:c:\dddd Feb 1, 2013 at 6:41
  • 2
    $ sign is for bash or unix variables. %% sign is for windows variables.
    – 0xC0DED00D
    Jun 26, 2013 at 14:59
  • If you have a SETLOCAL ENABLEDELAYEDEXPANSION in your batch file, you can use !EnvVariable! to expand it to its value during FOR loops, or other things that do not expand the macro's until the line is actually processed by the Cmd parser. See cmd /? for information on this, as well as help setlocal...
    – LarryF
    Apr 30, 2016 at 1:13
2

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.

2

Just to keep a default value of the variable. Press Enter to use default from the recent run of your .bat:

@echo off
set /p Var1=<Var1.txt
set /p Var1="Enter new value ("%Var1%") "
echo %Var1%> Var1.txt

rem YourApp %Var1%

In the first run just ignore the message about lack of file with the initial value of the variable (or do create the Var1.txt manually).

3
  • (set /p var=<var1.txt) 2>nul will suppres the errormessage. You can also give set var1=default instead of getting something from a file
    – Stephan
    Jan 25, 2017 at 9:01
  • Stephan, The second part of your proposal does not preserve the values between runs!? Can you develop it more?
    – Antoni
    Jan 25, 2017 at 9:22
  • no, not between runs. It just gives a default value. (there was no request to store the value between runs in the question).
    – Stephan
    Jan 25, 2017 at 10:06
1

One other way which might be interesting. You can call a powershell script from where you can do pretty much anything, and send the data bach to cmd or do other stuff with something like this.

set var=myvar; 
call "c:\input2.cmd" %var%. 

Its kind of more flexible (You can send the data the same way with powershell).

So in your cmd, write this considering the ps script is in C::

PowerShell.exe -ExecutionPolicy unrestricted -Command "& {. C:\Input.ps1}"

And in your input.ps1 script, write this:

$var = Read-Host -Prompt "Your input"
Write-Host "Your var:",$var
#Do stuff with your variable