2017-05-05 44 views
0

我想在計算機重新啓動,但我只當它沒有被(手動)12:00後(24小時)重新啓動。我不能拿到劇本與系統的系統信息工作| findstr/C:「系統啓動時間」雖然,它可以與%TIME%但該變量需要更改爲實際的重新啓動時間。如何批,「如果計算機最近重新啓動,則什麼也不做,否則重啓」

@echo off 
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set reboottime=%%a:%%b) 
if %reboottime% LSS 12:00 (
Goto :reboot) 
) else (
    GoTo :noreboot 
) 

::**************************************************************************************** 
:reboot 
shutdown /r /f /t 60 
) 
Exit /b 
::**************************************************************************************** 
:noreboot 
(echo 'no reboot required') 
Exit /b 
::**************************************************************************************** 
+0

要找出最後的引導時,請看看這裏:我如何才能找到當Windows上次重新啓動?(https://superuser.com/q/523726/146810 )答案中列出了幾種不同的方法。 – Matt

回答

2
@echo off 
    setlocal enableextensions disabledelayedexpansion 

    for /f "skip=1 tokens=1,3 delims=. " %%a in (' 
     "wmic os get LastBootUpTime,LocalDateTime" 
    ') do if not "%%b"=="" (
     set "bootUpTime=%%a" 
     set "currentTime=%%b" 
    ) 

    rem If last reboot was not today, reboot 
    if not "%bootUpTime:~0,8%"=="%currentTime:~0,8%" (
     shutdown /r /f /t 60 
    ) else (
     echo No reboot required 
    ) 

for /f命令執行wmic命令行工具來獲取最後的開機時間和本地時間。對於所使用的查詢的輸出格式是

LastBootUpTime    LocalDateTime 
20170417110928.382430+120 20170505132724.993000+120 

此輸出確定在for /f命令使用的選項。我們需要跳過標題行(skip=1),並分割使用的空間和點作爲分隔符(delims=.)的下一行,所以我們必須

   v   vv    v    delimiters 
20170417110928.382430+120 20170505132724.993000+120 
1 = %%a  2   3 = %%b  4    tokens 

通過reqesting令牌1和3,我們檢索兩個時間戳爲%%a(指示for替換參數)和%%b(下一個替換參數),我們將存入兩個變量來後只選擇日期(我們不能做替換參數的子字符串操作)。作爲wmic命令的輸出也包含結束空行,一個if命令被用來防止處理那些行。

使用兩個時間戳,我們只需要檢索並比較前八個字符(yyyymmdd)以查看上次啓動日期和當前日期是否匹配。如果沒有,重啓。

編輯看來我沒誤會指示小時。上面的代碼處理00:00/12:00 midnight情況。對於12:00 noon情況

@echo off 
    setlocal enableextensions disabledelayedexpansion 

    for /f "skip=1 tokens=1,3 delims=. " %%a in (' 
     "wmic os get LastBootUpTime,LocalDateTime" 
    ') do if not "%%b"=="" (
     set "bootUpTime=%%a" 
     set "currentTime=%%b" 
    ) 

    rem Calc how many days since last reboot 
    call :julianDate %currentTime:~0,4% %currentTime:~4,2% %currentTime:~6,2% ct 
    call :julianDate %bootUpTime:~0,4% %bootUpTime:~4,2% %bootUpTime:~6,2% bt 
    set /a "upTimeDays=ct-bt" 

    rem Assume we do not have to reboot and check 
    set "requireReboot=" 

    rem last reboot was today 
    if %upTimeDays% equ 0 (
     if "%currentTime:~8,6%" gtr "120000" if "%bootUpTime:~8,6%" lss "120000" set "requireReboot=1" 

    rem last reboot was yesterday 
    ) else if %upTimeDays% equ 1 (
     if "%bootUpTime:~8,6%" lss "120000" set "requireReboot=1" 
     if "%currentTime:~8,6%" gtr "120000" set "requireReboot=1" 

    rem last reboot was more than one day before 
    ) else (
     set "requireReboot=1" 
    ) 

    if defined requireReboot (
     echo shutdown /r /f /t 60 
    ) else (
     echo No reboot required 
    ) 

    goto :eof 

:julianDate year month day returnVar 
    setlocal enableextensions disabledelayedexpansion 
    set /a "d=100%~3%%100, m=100%~2%%100, a=(14-m)/12, y=%~1+4800-a, m=m+12*a-3" 
    set /a "jd=d+(153*m+2)/5+365*y+y/4-y/100+y/400-32045" 
    endlocal & set "%~4=%jd%" & goto :eof 
+0

_我的理解是,他們希望知道它是否從中午開始啓動,而不是在同一天。# – Compo

+0

'if「%bootuptime%」gtr「%currenttime:〜0.8%1200」...' – Stephan

+0

@Compo ,我把'12:00(24小時)'看作'00:00',但再讀一遍可能是你是對的,所以回答更新。謝謝 –

相關問題