2

I want to tell a user to input just how many minutes he wants before shutting down the computer and I don't know how can I avoid any letter in the user input. I just want to allow a number otherwise go to finish

@ECHO OFF

pushd %systemroot%
echo *{Enter the number of minutes  required before Turn off the computer then press (enter)}
set/p "x="


if %x% EQU %x% set/a z= 60*%x%
echo %z%
shutdown -s -t %z%


echo finish

2 Answers 2

2

Here is another method:

@ECHO OFF
pushd "%systemroot%"
:loop
"set x="
set /p "x=Enter the number of minutes required before Turn off the computer then press (enter) "
rem this allows the user to press enter to quit without doing anything
if not defined x goto :EOF
rem check for numerals only - else loop to start again
set "num="
for /f "delims=0123456789" %%a in ("%x%") do set "num=%%a"
if defined num goto :loop

set /a z=60*%x%
echo %z%
shutdown -s -t %z%

echo finish
Sign up to request clarification or add additional context in comments.

Comments

1
@ECHO OFF
SETLOCAL

pushd %systemroot%
echo *{Enter the number of minutes  required before Turn off the computer then press (enter)}
set/p "x="

SET "notnumber=9%x%"
FOR /l %%z IN (0,1,9) DO CALL SET "notnumber=%%notnumber:%%z=%%"
IF DEFINED notnumber ECHO entry was NOT a number&GOTO :EOF 

set/a z= 60*%x% 2>NUL
IF ERRORLEVEL 1 ECHO invalid number entered&GOTO :EOF 
echo %z%
ECHO shutdown -s -t %z%


echo finish
GOTO :eof

Note that batch regards a number that starts 0 as OCTAL, so entering 09 is invalid (because it's not octal) and entering 013 is valid, but will give a timeout of 11 minutes because 13 octal is 11 decimal.

Note also that the shutdown command is simply echoed to avoid shutting down during testing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.