The wonderful use of "set" in bat
In help documents in windows cmd and other introductions on the Internet, strings are generally replaced with the command "set". Or activate environment variables, but it is rare to use "set" and "if" to write an optional functional script. Here I will simply write a bat script with these two commands.
The @ rem This is an easy script to shutdwon and reboot with "set" command@echo script is used to implement the shutdown and restart options @ echo 1, shutdown @ echo 2, restart @ echo 3, cancel the above operations @ pause@echo offset / P var= Please enter your choice: if% var% = = 1 shutdown-s-t 5-c "It will shutdown after 5 seconds" if% var% = = 2 shutdown-r-t 5-c "It will reboot after 5 seconds" if% var% = 3 shutdown-a
Here are a few key commands that are useful in this script.
Set sets a variable for var, and "/ p" is the switch that receives the string we entered.
Note: when referencing a variable, pay attention to adding "%" on the left and right sides of the variable.
There are three uses of if
IF [NOT] ERRORLEVEL number commandIF [NOT] string1==string2 commandIF [NOT] EXIST filename command
The second use we use here, the string equivalent, is that in our bat, variables are almost equivalent to "strings". Our% var% variable is the string we entered, and when the string we enter is equal to 1, execute our subsequent command. In our case, this is the "shutdown" command.
The running effect of the script:
Here, in order to have a time buffer, I change the time to 30s, and if you want to terminate the script, run the script again. This is just to focus on the option usage of the set command.