Batch processing of automatic system shutdown can be realized

Time:2021-10-12

@ECHO off
TITLE   Automatic shutdown program   Author: Liao Xiaoqing

:start
CLS
COLOR 1f
rem   Use the color command to change the console output color
MODE con: COLS=41 LINES=18
rem   Mode statementsets the width and height of the form
set tm1=%time:~0,2%
set tm2=%time:~3,2%
set tm3=%time:~6,2%
ECHO  % date%  % TM1% point% TM2% minute% TM3% second
ECHO =========================================
ECHO   Select the action you want to perform and press enter
ECHO ───────────────
ECHO.
ECHO   one   Timed shutdown
ECHO   two   Countdown shutdown
ECHO   three   Delete scheduled shutdown task
ECHO   four   View task status
ECHO   five   Refresh current time
ECHO   six   Restart
ECHO   seven   lock computer
ECHO   eight   cancellation
ECHO   nine   sign out
ECHO.

:cho 
SET Choice=
SET  / P   Choice = select:
rem   Set the variable “choice” as the character entered by the user
IF NOT “%Choice%”==”” SET Choice=%Choice:~0,1%
rem   If the input is greater than 1 bit, take the first bit, such as input 132, and the return value is 1
ECHO.
IF /I “%Choice%”==”1” GOTO SetHour
IF /I “%Choice%”==”2” GOTO outtime
IF /I “%Choice%”==”3” GOTO delAt
IF /I “%Choice%”==”4” GOTO view
IF /I “%Choice%”==”5” GOTO start
IF /I “%Choice%”==”6” GOTO restart
IF /I “%Choice%”==”7” GOTO lock
IF /I “%Choice%”==”8” GOTO logoff
IF /I “%Choice%”==”9” GOTO end
rem   In order to avoid program exceptions caused by empty or blank return value, double quotation marks shall be added outside the variable
rem   Note that the if statement requires a double equal sign
rem   If the character entered is not the above number, it will return to re-enter
ECHO   Invalid selection, please re-enter
ECHO.
GOTO cho

:SetHour
CLS
ECHO.
SET ask=
SET  / p   Ask = is it set to execute shutdown command every day (Y / N):
IF NOT “%ask%”==”” SET ask=%ask:~0,1%
IF /I “%ask%”==”y” GOTO yes
IF /I “%ask%”==”n” GOTO no
GOTO SetHour

:yes
ECHO   Please specify a 24-hour time in the format   Hours: minutes
SET shutdowntime=
SET  / p   Shutdown time = Input:
at %shutdowntime% /every:M,T,W,Th,F,S,Su tsshutdn 0 /delay:0 /powerdown >nul
rem   Set as Monday to Sunday of each week, that is, every day
IF NOT errorlevel 1 GOTO ok
rem   If the input is correct, execute the statement of the OK segment
ECHO  % shutdowntime%   Not a standard time format, please re-enter
ECHO.
GOTO yes

:no
ECHO   Please specify a 24-hour time in the format   Hours: minutes
SET shutdowntime=
SET  / p   Shutdown time = Input:
at %shutdowntime% tsshutdn 0 /delay:0 /powerdown >nul
IF NOT errorlevel 1 GOTO ok
ECHO  % shutdowntime%   Not a standard time format, please re-enter
ECHO.
GOTO no

:ok
ECHO.
SET h=%shutdowntime:~1,1%
SET ah=%shutdowntime:~0,1%
SET am=%shutdowntime:~2,2%
SET bh=%shutdowntime:~0,2%
SET bm=%shutdowntime:~3,2%
IF “%h%”==”:” (
SET   HM =% ah% H% am%
) ELSE (
SET   HM =% BH% (H% BM% min)
rem   If you enter H: mm, then mm minute when HM = h, otherwise mm minute when HM = HH  
IF  / I   “%ask%”==”y”   ECHO   The system will shut down at% HM% every day
IF  / I   “%ask%”==”n”   ECHO   The system will shut down at% HM%
ECHO   Setup complete!   Press any key to continue
PAUSE >nul
GOTO start

:outtime
CLS
ECHO.
ECHO   Please enter the countdown seconds
ECHO ────────
ECHO   (to cancel after setting, click OK and press Ctrl + C twice)
SET timed=
SET  / p   Timed = Input:
tsshutdn %timed% /delay:0 /powerdown >nul
IF not errorlevel 1 GOTO ok
ECHO  % timed%   Is an invalid shutdown time, please re-enter
ECHO.
GOTO outtime

:delAt
cls
echo.
at /del /y
echo   The scheduled shutdown task has been cancelled. Press any key to continue
pause >nul
GOTO start

:view
MODE con: COLS=85 LINES=18
COLOR 70
ECHO.
at
ECHO   Press any key to continue
PAUSE >nul
GOTO start

:restart
shutdown -r -t 0

:lock
rundll32.exe user32.dll,LockWorkStation
goto start

:logoff
logoff

:end
exit