Automatic shutdown code implemented with bat

Time:2021-10-17

In general, we use shutdown to shut down and restart

Usage: shutdown [/ I | / L | / s | / R | / a | / P | / h | / E] [/ F]
[/m \\computer][/t xxx][/d [p:]xx:yy [/c “comment”]]

No parameters show help. This is different from typing /? It’s the same
/? Displays help. This is the same as not typing any options
/I displays the graphical user interface (GUI).
This must be the first option
/L log off. This cannot be used with the / m or / D options
/S turn off the computer
/R shut down and restart the computer
/A abandon system shutdown.
This can only be used during timeout
/P shut down the local computer without timeout or warning.
This can only be used with the / D option
/H hibernate the local computer.
This can only be used with the / F option
/E document the reasons for the unexpected shutdown of the computer
/M \ \ computer specifies the target computer
/T XXX sets the timeout before shutdown to XXX seconds.
The valid range is 0-600, and the default is 30
/C “comment” comment on the reason for restarting or shutting down.
Maximum 127 characters allowed
/F force running applications to shut down without warning the user in advance
/D [P:] XX: YY provide reasons for restart or shutdown
P indicates that the restart or shutdown is planned
XX is the primary reason number (positive integer less than 256)
YY is the secondary reason number (a positive integer less than 65536)

restart
shutdown /r /f

Shut down
shutdown /s /f

Let’s start with a simple functional version

Copy codeThe code is as follows:
@echo off
mode con lines=25
Title timed shutdown
color 1f
cls
echo.
echo.
echo.
echo.
echo               The time uses 24-hour system (such as 2:15 and 12:00), and multiple time points can be entered
echo.
echo                            Multiple time points are separated by spaces
echo.
echo.
echo ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
echo.
set times=
set /p times=                      Please enter the shutdown time:
: the following statement enables shutdown at a specified time every day of the week:
:: if you want to change to a few days of each month, please change letters to numbers and separate them with commas
:: there is no error detection statement in the following code. When entering the time, be sure to enter it in the specified format
:: you can use the at command in the CMD window to view the scheduled tasks
if not “%times%”==”” for %%i in (%times%) do (
    at %%i /every:M,T,W,Th,F,S,Su shutdown -s
)

The following is a multi-functional version, suitable for friends learning bat

Copy codeThe code is as follows:
@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 for
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 operation you want to perform and press enter
ECHO ───────────────
ECHO.
Echo 1. Timed shutdown
Echo 2. Countdown shutdown
Echo 3. Delete scheduled shutdown task
Echo 4. View task status
Echo 5. Refresh the current time
Echo 6. Restart
Echo 7. Lock the computer
Echo 8. Cancellation
Echo 9. Exit
ECHO.

:cho
SET Choice=
Set / P choice = select:
REM set the variable “choice” to 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, for example, 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 the return value being empty or containing spaces, double quotes should 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 selection is invalid, please re-enter
ECHO.
GOTO cho

:SetHour
CLS
ECHO.
SET ask=
Set / P ask = 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 the 24-hour time in the format of hour: minute
SET shutdowntime=
Set / P shutdown time = Input:
at %shutdowntime% /every:M,T,W,Th,F,S,Su tsshutdn 0 /delay:0 /powerdown >nul
Rem is set to 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% shutdown time% is not a standard time format, please re-enter
ECHO.
GOTO yes

:no
Echo please specify the 24-hour time in the format of hour: minute
SET shutdowntime=
Set / P shutdown time = Input:
at %shutdowntime% tsshutdn 0 /delay:0 /powerdown >nul
IF NOT errorlevel 1 GOTO ok
Echo% shutdown time% is 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% min
) ELSE (
Set HM =% BH% (H% BM% min)
REM if you enter H: mm, then HM = h mm min, otherwise HM = HHMM min
If / I “% ask%” = = “Y” echo system will shut down on% HM% every day
If / I “% ask%” = = “n” echo system will shut down at% HM%
Echo setting is 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 invalid shutdown time, please re-enter
ECHO.
GOTO outtime

:delAt
cls
echo.
at /del /y
Echo timed 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