Reading User Input

Maintained on

Sometimes you want a batch file to ask the user for something while it is running, such as a user name or the name of the folder to process.

In Command Prompt and batch files, you can read a string from the keyboard with set /p. This article covers the basic usage, then practical patterns such as checking for empty input and asking again when the input is invalid.

Basic usage of set /p

set /p uses the following syntax.

set /p variable=message to display

After the message is displayed, the user types a value and presses Enter, and the typed string is stored in the specified variable.

input.cmd
@echo off
setlocal

set /p name=Enter your name:
echo Hello, %name%

endlocal
pause

When you run it, it waits for input as shown below.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>input.cmd
Enter your name:Alice
Hello, Alice
C:\users\user>

You can reference the typed string with %name%, just like any other variable. For more about variables, see the following article.

Adding a trailing space to the message

Everything after the = is displayed as is. A trailing space is easy to miss in an editor, and without it the cursor sits right against the message.

If you enclose the variable name and the whole message in double quotes, the presence of the space becomes obvious.

set /p "name=Enter your name: "

The quotes themselves are not displayed. The samples below use this style.

Detecting empty input (just pressing Enter)

The most important thing to watch for with set /p is this: if the user presses Enter without typing anything, the variable is not emptied. It keeps its previous value.

@echo off
setlocal

set "name=unknown"
set /p "name=Enter your name: "
echo [%name%]

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>input.cmd
Enter your name:
[unknown]
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

You can use this behavior to implement a default value. In the example above, the initial value of name (unknown) acts as the default.

If you instead want to detect that nothing was entered, clear the variable before prompting. You can then check whether it is empty with if not defined.

@echo off
setlocal

set "name="
set /p "name=Enter your name: "

if not defined name (
  echo No name was entered.
  exit /b 1
)

echo Hello, %name%

endlocal

Writing set "name=" with nothing to the right of the = deletes the variable.

Clear the variable before prompting

If an environment variable with the same name already exists, pressing Enter alone leaves that value in place. When you need to detect empty input, always clear the variable with set "variable_name=" before set /p.

Validating the input

Confirming with Y/N

This example continues only when the user types y. The /i option makes the comparison case-insensitive, so y and Y are treated the same.

@echo off
setlocal

set "answer="
set /p "answer=Run the process? (y/n): "

if /i not "%answer%"=="y" (
  echo Cancelled.
  exit /b 1
)

echo Running the process.

endlocal

For details on the if command, see the following article.

If you only need a single key press, the choice command is a better fit. It does not require Enter and rejects any character you did not allow.

Checking that the input is a number

Before doing arithmetic with set /a, you may want to confirm that the input consists only of digits. You can use a regular expression with findstr.

@echo off
setlocal

set "num="
set /p "num=Enter a number: "

echo %num%| findstr /r "^[0-9][0-9]*$" > nul
if errorlevel 1 (
  echo That is not a number.
  exit /b 1
)

set /a result=num * 2
echo Twice %num% is %result%.

endlocal

There is no space before the | in echo %num%| because a space would become part of the string passed to findstr and prevent ^[0-9][0-9]*$ from matching.

This check does not look at the number of digits or the range. Also note that a number with a leading 0, such as 08, is treated as octal by set /a and causes an error.

findstr returns 0 as its exit code when it finds a match and 1 when it does not. For more on checking exit codes, see the following articles.

Asking again until the input is valid

If you want to ask again instead of exiting with an error, use goto to jump back to a label.

@echo off
setlocal

:INPUT
set "dir="
set /p "dir=Enter the target folder: "

if not defined dir goto INPUT

if not exist "%dir%\" (
  echo The folder "%dir%" does not exist.
  goto INPUT
)

echo Processing "%dir%".

endlocal

The folder check uses if exist "%dir%\" with a trailing \. Without the trailing \, the condition would also be true when a file with the same name exists.

When the input contains special characters

set /p itself stores the typed string in the variable as is. Characters such as & and | are not a problem at that point.

The problem appears when you expand the stored value with %name%. Because %name% is expanded before the line is parsed, any &, |, > or < in the value is treated as a command separator or redirection.

To handle such input safely, enclose the variable in double quotes or use delayed expansion. Quoting does not help if the input itself contains a ".

@echo off
setlocal enabledelayedexpansion

set "text="
set /p "text=Enter some text: "

echo !text!

endlocal

Delayed expansion (!text!) expands the value after the line has been parsed, so characters like & do not split the command. However, passing a string containing ! or ^ to another variable or to call with delayed expansion enabled can break it. If you need to handle truly arbitrary strings, consider using another language such as PowerShell.

Hiding input and suppressing the line break

Entering a password

Text typed at a set /p prompt is shown on screen as is. It is not suitable for something like a password, and there is no simple way to mask the input using batch commands alone.

If you really need this, consider another approach, such as PowerShell’s Read-Host -AsSecureString. It is also best to avoid writing passwords in plain text in a batch file.

Displaying a message without a line break

set /p is also used to display a message without waiting for input. If you specify nul as the input source, it prints the message without a line break and without waiting.

<nul set /p "=Processing..."
echo Done
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sample.cmd
Processing…Done
C:\users\user>

Because echo always ends with a line break, this is useful when you want the next output to continue on the same line.

When the script stops waiting for input

set /p waits for the user indefinitely. If you run the batch file in an environment where nobody is at the keyboard, such as Task Scheduler, it may never finish.

If a batch file may also run unattended, accept the value as an argument and prompt only when it is missing. That way both cases work.

@echo off
setlocal

set "name=%~1"
if not defined name set /p "name=Enter your name: "

echo Hello, %name%

endlocal

If an argument is passed, it is used. The prompt appears only when no argument was passed.

Reviewing several inputs at once

When you ask for many values, display everything the user entered at the end and continue only if they type y. This reduces mistakes caused by typos.

Practice questions

Question 1

What happens to the variable when the user presses only Enter at a set /p prompt?

回答がサーバーに送信されることはありません
Question 2

Which is best when you want the user to choose by pressing a single key, y or n, without pressing Enter?

回答がサーバーに送信されることはありません
#Command Prompt #Batch Files #set #Input #Commands