Skip to main content

How to Read Multi-line Input from the User in a Batch Script

The standard SET /P command is excellent for getting a single line of input from a user, but it stops reading the moment the user presses the Enter key. This makes it unsuitable for situations where you need to capture a multi-line block of text, such as a user's address, a detailed comment, or a block of code to be saved to a file.

This guide will teach you the standard workaround for this challenge. Since there is no native multi-line input command, you will learn how to use a temporary file and launch Notepad as a simple text editor, allowing the user to type freely across multiple lines. The script will then read the contents of this temporary file once the user saves and closes the editor.

The Challenge: SET /P is Single-Line Only

The SET /P command is designed to read a single line of text from the console. The Enter key is the signal that input is complete. There are no switches or parameters to change this behavior. Any attempt to get multi-line input must use a different strategy.

The Core Method: Using a Temporary File and Notepad

The most effective and user-friendly method is to delegate the text entry task to a real text editor. The logic is simple and robust:

  1. Create a unique temporary file.
  2. Provide instructions to the user in the command prompt.
  3. Launch Notepad (or another simple editor), opening the temporary file. The batch script will automatically pause and wait for Notepad to be closed.
  4. Read the content of the temporary file after the user has saved and closed Notepad.
  5. Clean up by deleting the temporary file.

This method allows the user to use a familiar interface for text editing and easily handles any amount of text, special characters, and line breaks.

The Script: A Basic Multi-line Input Capture

This script demonstrates the core logic. It opens an empty Notepad window and, after the user closes it, displays the text that was entered.

@ECHO OFF
SETLOCAL
SET "TempFile=%TEMP%\user_input_%RANDOM%.txt"

ECHO --- Multi-line User Input ---
ECHO.
ECHO A Notepad window will now open.
ECHO Please type your comments in the window, save your changes, and then close Notepad.
ECHO Your batch script will resume automatically after you close the editor.
ECHO.
PAUSE

REM --- Step 1: Create an empty temp file and launch Notepad ---
TYPE NUL > "%TempFile%"
START /WAIT notepad.exe "%TempFile%"

ECHO.
ECHO --- Input captured. Displaying contents: ---
ECHO ==============================================
TYPE "%TempFile%"
ECHO ==============================================
ECHO.

REM --- Step 4: Clean up the temporary file ---
DEL "%TempFile%"

ENDLOCAL
PAUSE

How the script works:

  • SET "TempFile=%TEMP%\... .txt": We create a unique filename in the user's temporary directory. This is a crucial best practice.
  • ECHO ...: Clear instructions are provided to the user.
  • TYPE NUL > "%TempFile%": This creates an empty, 0-byte file to ensure Notepad opens a new, blank document.
  • START /WAIT notepad.exe "%TempFile%": This is the heart of the script.
    • START: The command to launch a new process.
    • /WAIT: This is the most important switch. It forces the batch script to pause and wait for the application it launched (notepad.exe) to be completely closed by the user.
  • TYPE "%TempFile%": Once Notepad is closed, the script resumes. This command simply displays the full, multi-line content that the user typed and saved.
  • DEL "%TempFile%": A well-behaved script always cleans up its temporary files.

Common Pitfalls and How to Solve Them

  • User Doesn't Save: If the user closes Notepad without saving their changes, the temporary file will remain empty.

    • Solution: Your script can check if the file is still zero bytes after Notepad closes and prompt the user to try again if necessary.
      FOR %%F IN ("%TempFile%") DO IF %%~zF EQU 0 (ECHO No input was saved.)
  • Notepad is Already Open: The START /WAIT command will attach to the new Notepad process it creates. If the user has other Notepad windows open, it will not affect them.

  • Using a Different Editor: You can replace notepad.exe with any other simple text editor, such as notepad++.exe or write.exe (WordPad), if you know it is available on the target system.

Practical Example: A "Commit Message" Script

This script simulates a version control tool like Git. It needs to get a multi-line commit message from the user before proceeding.

This example is more robust. It provides a template inside the text file and then uses findstr to check if the user actually entered any non-comment lines.

@ECHO OFF
SETLOCAL
SET "CommitMsgFile=%TEMP%\commit_msg_%RANDOM%.txt"

ECHO --- FAKE-GIT COMMIT ---
ECHO.
ECHO Your code has been staged. Please provide a commit message.
ECHO An editor will open. Describe your changes, save, and close the window.
ECHO.
PAUSE

REM --- Create a template in the temp file for the user ---
(
ECHO.
ECHO # Please enter the commit message for your changes. Lines starting
ECHO # with '#' will be ignored.
) > "%CommitMsgFile%"

START /WAIT notepad.exe "%CommitMsgFile%"

ECHO.
ECHO --- Verifying commit message ---
REM We need to check if the user entered anything or just closed the window.
SET "MessageContent="
FOR /F "delims=" %%L IN ('findstr /V /B "#" "%CommitMsgFile%"') DO (
SET "MessageContent=%%L"
)

IF NOT DEFINED MessageContent (
ECHO [ERROR] Aborting commit due to empty commit message.
DEL "%CommitMsgFile%"
GOTO :End
)

ECHO [SUCCESS] Commit message captured. Proceeding with commit...
REM (Your commit logic would go here, using the content from %CommitMsgFile%)

DEL "%CommitMsgFile%"

:End
ENDLOCAL

Conclusion

While batch scripting has no direct command for multi-line input, the method of delegating the task to a text editor like Notepad is a simple, powerful, and universally understood solution.

The key components of this technique are:

  1. Create a unique temporary file in the %TEMP% directory.
  2. Launch the editor using START /WAIT notepad.exe "tempfile.txt". The /WAIT switch is essential.
  3. After the script resumes, read the contents of the temporary file using TYPE or a FOR /F loop.
  4. Always delete the temporary file before your script exits.