How can I trap an ENTER keystroke in batch? -
new site , scripting in general. aware can create user inputs this:
set input= set /p input=[y/n]: %=%
then can ask using "if" see if responses. example:
if "%input%"=="y" goto a:
and check if isn't said input saying
if "%input%" neq "y" goto b:
because i'm uneducated, don't know how accomplish this, want able determine if input enter keystroke. have tried accomplish using
if "%input%"==[enter] goto a:
and
if "%input%"==enter
and every method think of determine if keystroke [enter]. stupid, there way this? thanks.
there @ least 2 ways that: check value or check operation
set /p
retrieves typed input storing inside variable, when there no input, enter press, there no storage operation on variable keeps previous value.
the usual way test value clear variable contents, use set /p
, test if variable defined, is, has content
set "var=" set /p "var=[y/n]?" if not defined var goto :nouserinput
we can test operation itself. in batch files of commands set value internal errorlevel
variable indicating sucess or failure of execution of command.
in case of set /p
set errorlevel
0 if data has been retrieved , set 1 if there no input. if errorlevel
construct can used determine if there input
set /p "var=[y/n]?" if errorlevel 1 goto :nouserinput
as aacini points, can fail. if batch file has .bat
extension set /p
reads data not reset (set 0) previous errorlevel
value, necessary first execute command set errorlevel
0.
ver > nul set /p "var=[y/n]?" if errorlevel 1 goto :nouserinput
if batch file saved .cmd
extension not needed. in case set /p
set errorlevel
0 when reads data.
also conditional execution operators (&&
, ||
) can used. &&
executes next command if previous 1 sucessful. ||
execute next command if previous 1 failed.
set /p "var=[y/n]?" || goto :nouserinput
Comments
Post a Comment