Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this batch file "remember" user input?

I've been writing a batch file which uses the Chocolatey package manager to install software to a pc. I've written it in a way that gives the user a choice for installing each piece of software. By default, every package question should use N as an the input if the enter key is pressed without user input. This should skip the step and move onto the next.

My issue is that if I press y to accept one of the packages but then press enter on the next package without typing anything, instead of it defaulting to N it keeps the y from the previous request.

Example:

Would you like to install software 1?(y/N) [return]
Skipping software

Would you like to install software 2?(y/N) y
Installing software 2

Would you like to install software 3?(y/N) [return]
Installing software 3

It seems to be keeping the last key pressed in memory. Is there a way to clear the last pressed key? or some other fix. If I go through the entire process without pressing any letter keys and only press return it skips everything. But if I press y even once every single question after that y also gets accepted.

Preview of the code below:

:choose_mbam
ECHO.
SET /P c=Would you like to install malwarebytes anti-virus?(y/N)
IF /I "%c%" EQU "Y" ( 
  GOTO :install_mbam
) ELSE IF /I "%c%" EQU "N" ( 
  GOTO :choose_plugins 
) ELSE ( 
  GOTO :choose_plugins
)

:install_mbam
ECHO Installing Malwarebytes
powershell -command "& cinst malwarebytes -y">NUL || ECHO Malwarebytes already installed and up to date.
GOTO :choose_plugins

:choose_plugins
ECHO.
set /P c=Would you like to update your plugins eg.flash, silverlight, java?(y/N)
if /I "%c%" EQU "Y" ( 
  goto :install_plugins
) ELSE IF /I "%c%" EQU "N" ( 
  goto :choose_browser
) ELSE ( 
  GOTO :choose_browser 
)

:install_plugins
ECHO Installing Silverlight
powershell -command "& cinst silverlight -y">NUL
ECHO.
ECHO Installing Flash
powershell -command "& cinst flashplayerplugin -y">NUL
ECHO.
ECHO Installing Java
powershell -command "& cinst jre8 -y">NUL
ECHO.
ECHO Installing .Net 4.5
powershell -command "& cinst dotnet4.5.1 -y">NUL
GOTO :choose_browser

:choose_browser
...

EDIT: Answer was shown to me by Stephan in the comments. As I said, I didn't realise that "set /p c=" was a way of setting a permanent variable rather than asking a question for user input (which was what the example someone gave to me made it out to be) so i've now replaced all instances of that with "ECHO" so it works fine! Can't select best answer as answer was in a comment.

like image 847
Bill Proxima Chandler Avatar asked Dec 27 '25 18:12

Bill Proxima Chandler


1 Answers

set /p leaves the value of the variable unchanged, if the user just presses ENTER. You can use this behaviour to predefine a default value before asking the user:

set c=N
SET /P c=Question?(y/N) 

If you don't want a Default value, set the Default value to nothing:

set "c="
SET /P c=Question?(y/n) 
if not defined c echo no Input given
like image 55
Stephan Avatar answered Dec 30 '25 22:12

Stephan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!