Is there a way to have a multi line user input prompt in batch?

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

In batch scripting, there is no built-in way to have a multi-line user input prompt using standard batch commands. However, you can work around this limitation by using a combination of techniques such as temporary files or variables. Here's an example of how you can achieve a multi-line user input prompt:

batch
@echo off
setlocal EnableDelayedExpansion

REM Define temporary file path
set "tempFile=%temp%\input.txt"

REM Prompt the user
echo Enter multiple lines of input. Press Ctrl+Z (Windows) or Ctrl+D (Linux/macOS) followed by Enter to finish.

REM Create a temporary file for input
type nul > %tempFile%

REM Read input lines and append them to the temporary file
:loop
set "line="
set /p "line="
if not defined line goto :eof
echo !line!>> %tempFile%
goto loop

REM Read the contents of the temporary file
echo.
echo The input lines are:
type %tempFile%

REM Cleanup - delete the temporary file
del %tempFile%

In the above example, the script prompts the user to enter multiple lines of input. The user can input lines one by one, and pressing Ctrl+Z (Windows) or Ctrl+D (Linux/macOS) followed by Enter signals the end of input. The input lines are then stored in a temporary file (%tempFile%).

Afterward, the script reads and displays the contents of the temporary file. Finally, the temporary file is deleted to clean up.

Please note that using temporary files for multi-line input can have security and usability implications, especially if sensitive data is involved. Consider the specific requirements and constraints of your use case before implementing this approach.