批处理 自动关机

批处理 自动关机


This is a piece of code that I wrote after self-study batch for a week, and the test result seems ok. So mark this to encourage self.

// An highlighted block
@ECHO OFF
@REM ------------------------------------------------------------------------------------------------------------------------
@REM  (C) 2020 .
@REM  This is a initial version.
@REM  
@REM  @author  jwang66
@REM  @Version 
@REM  @Nov 15th, 2020, Sunday
@REM  Purpose: The batch file will be shutting down machine, including 3 methods such as shutdown without delay, 
@REM  Countdown shutdown, and shutdown at specific time. certainly user could cancel shutdown in the running process.
@REM ------------------------------------------------------------------------------------------------------------------------

setlocal EnableDelayedExpansion

COLOR 0B

ECHO Hi do you wanna shutdown your computer now..? && ECHO the following options you could select.
ECHO.
ECHO - [1] [shutdown now wihtout delay.]
ECHO.
ECHO - [2] [shutdown as your typed time.]
ECHO.
ECHO - [3] [shutdown as countdown time elapsed.]
ECHO.
ECHO - [4] [restart system.]
ECHO.
ECHO - [5] [cancel shutdown operation.]
ECHO.
ECHO please enter option num:
ECHO Off
@REM WAITING FOR USER TO TYPE IN COMMAND OPTION
SET /P OPTION_CMD= 
  CALL :SHUTDOWN %OPTION_CMD:~0,1%
  ECHO.
  CALL :USER_TIP
GOTO :END


:SHUTDOWN
IF 1==%1 shutdown -s || GOTO :FAILBUILD
IF 2==%1 (
  ECHO CURRENT TIME: %TIME% , When do you wanna shutdown, ECHO. Please detail time [format as '22:00']  &&  SET /P DETAIL_TIME=
  CALL :SHUTDOWN_AT !DETAIL_TIME!
)
IF 3==%1 (
  ECHO Please detail countdown time[format as 2s/2m/2h meaning 2secs/22min/2hours]  &&  SET /P COUNTDOWN_TIME=
  CALL :SHUTDOWN_COUNTDOWN !COUNTDOWN_TIME!
)
IF 4==%1 (shutdown -r)
IF 5==%1 (shutdown -a)
)
GOTO :EOF

:SHUTDOWN_AT
at %1 shutdown -s
GOTO :EOF

:SHUTDOWN_COUNTDOWN
@REM Check if unit what is(only read last one character in COUNTDOWN_TIME)
SET COUNTDOWN_TIME=%1 && SET UNIT_CHECK=%COUNTDOWN_TIME:~-1%
@REM Replacement string of "s" to "" for COUNTDOWN_TIME
SET COUNTDOWN_CHECK=!COUNTDOWN_TIME:s=!

@REM If unit is not second, go on check if unit is minute.
IF !COUNTDOWN_CHECK!==!COUNTDOWN_TIME! SET COUNTDOWN_CHECK=!COUNTDOWN_TIME:m=!

@REM If unit is not second, go on check if unit is hour.
IF !COUNTDOWN_CHECK!==!COUNTDOWN_TIME! SET COUNTDOWN_CHECK=!COUNTDOWN_TIME:h=!

@REM If unit is second, shutdown machine.
IF /i s==!UNIT_CHECK! shutdown -s -t  !COUNTDOWN_CHECK!

@REM If unit is min or hour, convert min to seconds and shutdown machine.
IF /i m==!UNIT_CHECK!   SET /a COUNTDOWN=!COUNTDOWN_CHECK!*60 && shutdown -s -t    !COUNTDOWN! 
IF /i h==!UNIT_CHECK!   SET /a COUNTDOWN=!COUNTDOWN_CHECK!*3600 && shutdown -s -t  !COUNTDOWN!
GOTO :EOF

:USER_TIP
@REM Tip for user to cancel shutdown anytime.
ECHO The shutdown process is running now, you could cancel this operation anytime, [Y/N]
SET /p OPTION_CMD=
IF /i Y==%OPTION_CMD:~0,1% CALL :SHUTDOWN 5 ELSE EXIT
GOTO :EOF

GOTO :END
:FAILWITHMESSAGE
echo ERROR: The DataPackager Tool execution failed!
exit /B 1


:END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值