2016-04-02 61 views
1
set /p RESTART= WOULD YOU LIKE TO RESTART YOUR COMPUTER NOW?(Y/N): 
    echo.  

if %RESTART%==Y (
       echo. 
       echo. 
       echo YOUR COMPUTER WILL BE REBOOTED IN 30 SECONDS!!! 
       shutdown.exe /r /t 30 
       goto Ryes 
      ) else (
        echo. 
        echo. 
        echo YOU CHOSE NOT TO RESTART YOUR COMPUTER. 
        echo. 
        echo *****REMEMBER THAT YOU STILL NEED TO RESTART YOUR COMPUTER SO THE CHANGES CAN BE APPLIED! ***** 
        ) 

    :Ryes 
    echo. 
    set /p ABORT= WOULD YOU LIKE TO ABORT THE REBOOT?(Y/N): 
    echo. 

    :A 
    shutdown /a 

    pause 

「您選擇不重新啓動計算機」的消息仍然出現,如果我鍵入Y.關機命令行和郵件問題

關機命令不工作,我運行.bat爲管理員。 嘗試了Windows XP和最新的Windows版本。

我輸入y不是Y,但我不認爲這是問題,因爲其他用戶輸入選擇工作只是罰款大小寫。 都關機命令:

shutdown -r -f -t 30 
shutdown.exe /r /t 30 

我知道,因爲我在嘗試使用中止選項時,這條消息:

Unable to abort the system shutdown because no shutdown was in progress.(1116) 
Press any key to continue . . . 

什麼是我的批處理文件的問題?

謝謝你的所有幫助和時間。

+1

您輸入'Y'而不是'y',對不對? – SomethingDark

回答

1

更多有關IF

您應該修改這一行之前: if %RESTART%==Y此行 If /I "%RESTART%"=="Y"

/I做一個案例Insens itive字符串比較。

@echo off 
set /p RESTART= WOULD YOU LIKE TO RESTART YOUR COMPUTER NOW?(Y/N): 
echo.  
If /I "%RESTART%"=="Y" (
    echo. 
    echo. 
    echo YOUR COMPUTER WILL BE REBOOTED IN 30 SECONDS!!! 
    shutdown.exe /r /t 30 
    goto Ryes 
    ) else (
    echo. 
    echo. 
    echo YOU CHOSE NOT TO RESTART YOUR COMPUTER. 
    echo. 
    echo *****REMEMBER THAT YOU STILL NEED TO RESTART YOUR COMPUTER SO THE CHANGES CAN BE APPLIED! ***** 
) 
pause 
+0

@ Fred22將'=='改爲'equ'不是必需的。對於「IF」操作數而言,這是一種不好的編程習慣,尤其是當值來自用戶輸入時。您需要將其編碼爲:'if/I「%RESTART%」==「Y」' –

0

添加此您檢查RESRART變量被定義爲Y

if %RESTART%==y set restart=Y 
+0

這是不必要的; 'if'可以選擇進行不區分大小寫的比較。 – SomethingDark