0

I'm trying to make a script to shutdown my computer.

It asks for input wether the value will be in minutes or hours, in order to convert them to seconds for the shutdown command.

This is what my script looks like and it doesn't work.

@echo off
color 0a
:home
echo Hours (h) or minutes (m)?
set /p unit=

if "%unit%"=="h" (
    echo For how long do you want to delay it?
    set /p value=
    set /a secs="value*=60*60"
    ) if "%unit%"=="m" (
    echo For how long do you want to delay it?
    set /p value=
    set /a secs="value*=60"
    ) else (
    goto:home
    )

rem shutdown /s /f /t %secs%
echo %secs%

echo Closing in 3 seconds.
ping 127.0.0.1 -n 4 > nul

If anyone could lighten me up or come up with a better way to achieve it (which I'm sure exists), it'd be great.

2 Answers2

0

I feel stupid now.

I totally forgot about else if.

Anyways, here's the finished working script. Also, input is now case-insensitive.

@echo off
echo ****************************
echo *                          *
echo * QUICK SHUTDOWN SCHEDULER *
echo *                          *
echo ****************************
echo.
echo Hours (H) or minutes (M)?
:home
set /p unit=

if /I "%unit%"=="h" (
    echo For how many hours do you want to delay it?
    set /p value=
    set /a secs="value*=60*60"
) else if /I "%unit%"=="m" (
    echo For how many minutes do you want to delay it?
    set /p value=
    set /a secs="value*=60"
) else (
    echo Please only type H or M.
    goto:home
)

shutdown /s /f /t %secs%

echo Closing in 3 seconds.
ping 127.0.0.1 -n 4 > nul
0

You may use choice command to get the input from user instead set /P; this way, you don't even need to check the provided input because it always be correct. For further details type: choice /?.

You may also use an array to show a messsage with the right units name without using an if to identify it; see this post for further details.

@echo off
setlocal EnableDelayedExpansion

rem Define the "units" array
set "units[1]=minutes"
set "units[60]=hours"

choice /N /C MH /M "Hours (H) or minutes (M)? "
set /A unit=(%errorlevel%-1)*59 + 1
set /p "value=For how many !units[%unit%]! do you want to delay it? "

set /a "secs=value* unit *60"

shutdown /s /f /t %secs%

echo Closing in 3 seconds.
ping 127.0.0.1 -n 4 > nul
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108