|
Answer» My script:
echo off echo. echo Please enter one of the listed options: echo. echo Select "S" or "s"to shut down. echo echo **************************************************** echo. echo Select "R" or "r"to restart. echo echo **************************************************** echo. echo Select "E" or "e"to exit this screen. echo. echo **************************************************** echo. set /p choice=Select choice and enter: set choice=%choice:~0,1% if /I "%choice%"=="S" goto SHUTDOWN if /I "%choice%"=="R" goto restart if /I "%choice%"=="E" exit
:shutdown shutdown -s -t 10
:restart shutdown -r -t 0
I save it as Run.bat
In WinXP it runs perfectly.
But in Win7, when i enter S for shut down, it runs shutdown (can see the warning that computer is shutting down in less than a minute) and follow by restart and it ended up restart.
Any idea?Possibly this may be because in Vista and later you need to be an administrator or a user who has been assigned the Shut down the system user right.
You don't need those labels. Why don't you just do this? (then you won't have the code falling through to the next label and you may be able to see any error messages)
Code: [Select] set /p choice=Select choice and enter: set choice=%choice:~0,1% if /I "%choice%"=="S" shutdown -s -t 10 if /I "%choice%"=="R" shutdown -r -t 0 exit
But by running shutdown -s -t 10
from cmd works fine.See my edited post above ok thanks.
Btw if i put it like this, it shuts down fine.
:restart shutdown -r -t 0
:shutdown shutdown -s -t 10 The reason for this is simple. The batch script triggers the shutdown process, but does not wait for it to complete. In XP you cannot start a second shutdown if one is already running, in Windows 7 the second one replaces the first if the delay has not expired.
Do you see why I suggested that you avoid using labels in this script? Of course you can put an EXIT command in the right place. Or SWITCH the restart and shutdown sections.
By the way, you have lines starting echo (no dot) which will trigger "echo is off" messages.
set /p choice=Select choice and enter: set choice=%choice:~0,1% if /I "%choice%"=="S" goto shutdown <------ If you select S these things happen: if /I "%choice%"=="R" goto restart if /I "%choice%"=="E" exit
:shutdown <------ Execution is directed to this label shutdown -s -t 10 <------ Shutdown -s process starts with 10 second delay <------ Batch does not wait. Execution returns here. :restart <------ Execution falls through to this label shutdown -r -t 0 <------ In XP this is barred, in Windows 7 a Shutdown -r process starts with no delay and restart is triggered.
This shows something you have to remember if you use goto with labels: you have to manage what happens after you have gone to the label and executed the command or COMMANDS after that label. Also you have to manage what happens if none of the IF tests are satisfied.
set /p animal="Type cat or dog " if /i "%animal%"=="cat" goto cat if /i "%animal%"=="dog" goto dog
if you TYPED anything other than cat or dog you will still get here :cat echo You typed cat
if you typed cat you will still get here :dog echo You typed dog
If you typed "fish" the batch will show
You typed cat You typed dog
If you typed "cat" the batch will show
You typed cat You typed dog
If you typed "dog" the batch will show
You typed dog
This shows how to manage (a) wrong input (B) correct input:
:loop set /p animal="Type cat or dog " if /i "%animal%"=="cat" goto cat if /i "%animal%"=="dog" goto dog echo Type cat or dog only! goto loop
:cat echo You typed cat goto end
:dog echo You typed dog
:end
|