Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use bat script to realize automatic shutdown

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use bat script to achieve automatic shutdown". In daily operation, I believe many people have doubts about how to use bat script to achieve automatic shutdown. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to use bat script to achieve automatic shutdown". Next, please follow the editor to study!

Normally, we use shutdown to shut down and restart the computer.

Usage: shutdown [/ I | / l | / s | / r | / a | / p | / h | / e] [/ f]

[/ m\\ computer] [/ t xxx] [/ d [p:] xx:yy [/ c "comment"]]

There are no parameters to display help. This has to do with typing /? It's the same.

/? Show help. This is the same as not typing any options

/ I display graphical user interface (GUI).

This must be the first option.

/ l log out. This cannot be used with / m or / d options

/ s turn off the computer

/ r shut down and restart the computer

/ an abort system shutdown.

This can only be used during a timeout

/ p shut down the local computer with no 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 reason for the unexpected shutdown of the computer

/ m\\ computer specifies the target computer

The timeout before / t xxx setting is turned off is xxx seconds.

Valid range is 0-600, default is 30

/ c comment on the reason why "comment" restarts or shuts down.

Maximum 127 characters allowed

/ f force the running application to close without warning the user

/ d [p:] xx:yy provides reasons for restarting or shutting down

P indicates that the restart or shutdown is planned

Xx is the main reason number (a positive integer less than 256)

Yy is the secondary reason number (a positive integer less than 65536)

Restart

Shutdown / r / f

Shutdown

Shutdown / s / f

Let's start with a simple functional version.

The code is as follows:

@ echo off

Mode con lines=25

Title shuts down regularly

Color 1f

Cls

Echo.

Echo.

Echo.

Echo.

Echo time uses a 24-hour system (such as 2:15, 12:00), and you can enter multiple time steps

Echo.

Multiple time points in echo are separated by spaces

Echo.

Echo.

Echo ━━

Echo.

Set times=

Set / p times= Please enter shutdown time:

The following statement shuts down the computer at a specified time every day of the week

:: if you want to change to certain days of each month, please change letters to numbers and separate them with commas

:: there is no error detection statement in the following code. Be sure to enter the time in the specified format.

:: you can use the at command in the CMD window to view 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 who learn bat.

The code is as follows:

@ ECHO off

TITLE automatic shutdown program author: Liao Xiaoqing

: start

CLS

COLOR 1f

Rem uses the COLOR command to change the console output color

MODE con: COLS=41 LINES=18

The rem MODE statement sets 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% points tm2% minutes tm3% seconds

ECHO = =

ECHO, please select the operation you want to perform, and then press enter

ECHO ─

ECHO.

ECHO 1. Timing shutdown

ECHO 2. Countdown to 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. Write off

ECHO 9. Quit

ECHO.

: cho

SET Choice=

SET / P Choice= selection:

Rem sets 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, for example, enter 132, then 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

In order to avoid program exceptions caused by empty return values or spaces in rem, it is necessary to put double quotation marks around the variables.

Rem note that the IF statement requires a double equals sign

Rem if the character entered is not the above number, it will be returned to re-enter

Invalid ECHO selection, please re-enter

ECHO.

GOTO cho

: SetHour

CLS

ECHO.

SET ask=

Whether SET / p ask= is set to execute the shutdown command every day (yzone):

IF NOT "% ask%" = "" SET ask=%ask:~0,1%

IF / I "% ask%" = = "y" GOTO yes

IF / I "% ask%" = = "n" GOTO no

GOTO SetHour

: yes

For ECHO, please specify a 24-hour standard time in hours: minutes.

SET shutdowntime=

SET / p shutdowntime= input:

At shutdowntime% / every:M,T,W,Th,F,S,Su tsshutdn 0 / delay:0 / powerdown > nul

Rem is set every week from Monday to Sunday, that is, every day

IF NOT errorlevel 1 GOTO ok

Rem executes the statement of the ok section if it is entered correctly

ECHO shutdowntime% is not a standard time format, please re-enter it

ECHO.

GOTO yes

: no

For ECHO, please specify a 24-hour standard time in hours: minutes.

SET shutdowntime=

SET / p shutdowntime= input:

At shutdowntime% tsshutdn 0 / delay:0 / powerdown > nul

IF NOT errorlevel 1 GOTO ok

ECHO shutdowntime% is not a standard time format, please re-enter it

ECHO.

GOTO no

: ok

ECHO.

SET hobbies% shutdowntimelug 1%

SET ah=%shutdowntime:~0,1%

SET am=%shutdowntime:~2,2%

SET bh=%shutdowntime:~0,2%

SET bm=%shutdowntime:~3,2%

IF "% h%" = ":" (

% am% points at SET HM=%ah%

) ELSE (

% bm% points at SET HM=%bh%)

Rem if you enter h:mm, then mm points for HM=h, otherwise mm points for HM=hh

IF / I "% ask%" = "y" ECHO system will be shut down at% HM% every day

IF / I "% ask%" = "n" ECHO system will be shut down at% HM%

ECHO is set! 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 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

At this point, the study on "how to use bat script to achieve automatic shutdown" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report