- 1 year ago
Windows batch file - automatic answer while executing batchfile through CMD prompt
To automatically provide answers or inputs to prompts while executing a Windows batch file through the Command Prompt, you can make use of input redirection and echo commands. Here's an example:
Assuming you have a batch file called script.bat
that prompts for user input:
batch@echo off
echo Enter your name:
set /p name=
echo Hello, %name%!
To automatically provide the name without user input, you can create a text file called answers.txt
with the desired input:
John Doe
Then, modify your batch file to read the input from the answers.txt
file using input redirection (<
):
batch@echo off
< answers.txt (
echo Enter your name:
set /p name=
)
echo Hello, %name%!
Now, when you execute the batch file through the Command Prompt, it will read the input from the answers.txt
file and use it to provide the name without requiring user interaction.
To execute the modified batch file:
bashC:\> script.bat
Note: Ensure that the answers.txt
file is in the same directory as the batch file, or provide the full path to the answers.txt
file if it is located in a different directory.
By utilizing input redirection and a text file containing the desired answers, you can automate the input process in Windows batch files executed through the Command Prompt.