HOWTO · Batch

How to Prompt User Input and Use Results in Batch Script

This tutorial teaches you how to prompt user input and utilize the results in batch scripts. Learn to create interactive scripts that enhance user experience by capturing and validating input effectively. Explore practical examples and advanced techniques to automate tasks seamlessly.

On this page

A Windows CMD batch script can ask the person running it for text, store the answer in a variable, and use that answer in later commands. The set /p command does all three: it prints a prompt, waits for one line of input, and assigns the typed text to a variable. This guide shows how to prompt for input, use the value in a command, validate it, and repeat the prompt when the answer is not usable.

Prompt for input with set /p

The general form is set /p "variable=Prompt text: ". The command writes the prompt text to the console without a trailing newline, reads one line up to Enter, and stores it in variable. The prompt text is optional: set /p "name=" waits for input while printing nothing. Quoting the whole assignment, as in set /p "var=...", keeps spaces and characters such as & inside the prompt text from being parsed as commands.

Save these lines as greet.bat and run the file from a command prompt:

@echo off

set /p "userName=Please enter your name: "

echo Hello, %userName%!

When you run the script and type John at the prompt, the console shows the prompt and the typed answer on the same line and then prints Hello, John!. The answer is stored in userName and stays available until the script ends; %userName% expands to the typed text wherever it appears later in the script.

Because set /p is a cmd.exe builtin, the scripts in this guide run only on Windows. They are shown as syntax you can save and run yourself; the text after each script describes what a typical interactive run looks like rather than captured output.

Use the entered value in a command

Because the answer lives in a normal environment variable, it can drive a later command: choose a directory, a file name, a search term, or an option. The example below asks for a folder name and changes into it. Two details keep it safe:

  • The variable is used quoted ("%dirName%"), so a folder name containing spaces still works.
  • if exist confirms the folder before cd runs, so a mistyped name produces a clear message instead of a failed script.

@echo off

set /p "dirName=Enter a folder name (for example Documents): "

if exist "%dirName%\" (

cd /d "%dirName%"

echo You are now in %cd%.

) else (

echo Could not find a folder named "%dirName%".

)

On a typical run, typing Downloads changes into that folder and prints a line such as You are now in C:\Users\Ada\Downloads. — whatever the current directory happens to be. Typing a name that does not match a folder prints Could not find a folder named "name" with the typed text in place of name.

"%dirName%\" tests the answer as a directory; without the trailing backslash, if exist would also accept a file with that name. cd /d also switches drive letters when the answer is a path on another drive.

Validate input and handle errors

A user can press Enter without typing, type text where a number is expected, or supply a value the script cannot use. Check the variable before acting on it and say clearly what went wrong.

The example asks for an age and applies two checks:

  • set "age=" clears the variable first, because set /p leaves it unchanged when Enter is pressed alone, and if not defined then catches that case.
  • findstr /r "^[0-9][0-9]*$" confirms the answer contains only digits; if errorlevel 1 means it does not.

@echo off

set "age="

set /p "age=Enter your age: "

if not defined age (

echo No value was entered.

goto :eof

)

echo %age%|findstr /r "^[0-9][0-9]*$" >nul

if errorlevel 1 (

echo "%age%" is not a whole number.

) else (

echo Age recorded: %age%.

)

Typing twelve makes the script print "twelve" is not a whole number. Typing 30 prints Age recorded: 30., and pressing Enter without typing anything prints No value was entered.

For checks a string comparison can express, quote both sides: if /i "%var%"=="yes". For “did the previous command fail” checks, if errorlevel 1 reads the exit code. When the script cannot continue, goto :eof ends it cleanly; use exit /b 1 only when a calling script needs a nonzero status.

Repeat the prompt until input is acceptable

A label plus goto turns a prompt into a loop: jump back to the label whenever validation fails. Count the attempts and stop after a few tries so the script also finishes when no one is answering, for example when input comes from a file or a pipe instead of a keyboard.

@echo off

setlocal

set "tries=0"



:askNumber

set /a "tries+=1"

if %tries% gtr 3 (

echo No usable answer after 3 attempts. Exiting.

goto :eof

)

set "num="

set /p "num=Please enter a whole number between 1 and 10: "

if not defined num (

echo Nothing was entered.

goto askNumber

)

echo %num%|findstr /r "^[0-9][0-9]*$" >nul

if errorlevel 1 (

echo "%num%" is not a whole number.

goto askNumber

)

if %num% lss 1 (

echo %num% is below the accepted range of 1 to 10.

goto askNumber

)

if %num% gtr 10 (

echo %num% is above the accepted range of 1 to 10.

goto askNumber

)

echo You entered a valid number: %num%.

A typical session works like this: the script asks for a number, fifteen is answered with "fifteen" is not a whole number. and the prompt repeats, 15 is answered with 15 is above the accepted range of 1 to 10. and the prompt repeats, and 5 is accepted with You entered a valid number: 5. After three unsuccessful tries the loop stops with No usable answer after 3 attempts. Exiting.

lss and gtr compare numbers, and findstr has already confirmed the answer is all digits, so 2 correctly falls inside the range of 1 to 10. A quoted comparison such as if "%num%" leq "10" compares text instead and would wrongly reject single digits above 1. setlocal keeps tries and num from leaking into the calling environment.

Limits and safe handling of user input

  • Windows CMD only. set /p is a cmd.exe builtin; the method described here does not apply to PowerShell or Unix shells. In PowerShell, use Read-Host instead; in bash, read -p does the same job.
  • One line, plain text. It reads a single line up to Enter. There is no masked input for secrets and no cursor editing beyond Backspace.
  • No answer keeps the old value. Pressing Enter alone, or running the script with no keyboard input, leaves the variable unchanged. Clear it with set "var=" before prompting and test if not defined afterward.
  • Special characters survive but can break later commands. Text containing &, |, <, >, or " is stored literally, so use the variable quoted ("%var%") in commands and comparisons, and validate before the value reaches a command line.
  • Simple input only. Very long answers and complex rules quickly outgrow if and findstr. For fixed-option menus, choice /c yn /m "Continue" is simpler; for richer interaction, a PowerShell script is a better fit.

Run this example, which shows the same prompt-and-answer flow with the bash read -p builtin; the here-string <<< "John" plays the role of typing John and pressing Enter:

read -r -p "Please enter your name: " userName <<< "John"
echo "Hello, $userName!"
Hello, John!

On a real terminal, read -p displays the prompt text before reading; the printed greeting is the same result the batch script produces with set /p and echo.

Used together — capture with set /p, clear and validate the variable before use, retry with a bounded goto loop, and quote it in every command — these techniques cover most interactive batch tasks reliably.