1

I have a batch file which requires 2 user inputs.

@echo Off
setlocal

echo.
echo Please select an opt:
echo.
echo 1
echo 2
echo.
set /p opt= Variant Number (1-2)?
echo.
echo opt selected is %opt%"

echo Please Select opt2:
echo.
echo 4
echo 5
echo.
set /p opt2= Variant Number (4-5)?
echo.
echo opt selected is %opt2%
goto end

:end
pause
endlocal

I want to invoke this batch file from a C# program and input 1 and 5 programmatically.

I tried (echo 1&&echo 5)|test.bat. But it is not working, giving output as blank for the 4-5 selection. This is working fine if there is only one input; like echo 1|test.bat.

This batch file is fixed and I don't have rights to modify it. So method to simulate the user input is expected.

FaisalM
  • 724
  • 5
  • 18
  • The parameters are passed like normal on the command line. In the batch file you extract them from %1 and %2 etc. see linked question. – Andrew Truckle Apr 11 '22 at 13:27
  • I don’t get why you have a batch designed for two prompts and then user interaction. Then you want it automated. My previous comment would work. But I don’t know when it is in a batch wanting user input. – Andrew Truckle Apr 11 '22 at 13:33
  • 1
    @Andrew Here the batch file is designed to take input from the user. I cannot change that. Is there any way to simulate multiple user inputs from a program? – FaisalM Apr 11 '22 at 13:35
  • 2
    @Andrew Truckle This is not a duplicate of the question you tagged. The question is different. Kindly remove the 'close' request. – FaisalM Apr 11 '22 at 13:40

1 Answers1

3

Create a text file with the desired values. Put each on their own line and be sure to press the Enter key after each value. Then your command line will be, for example:

test.bat < values.txt
EddieLotter
  • 324
  • 3
  • 8
  • The problem is that batch files can't read multiple lines from a pipe. But it works when using a redirection. – jeb Apr 14 '22 at 10:06