2017-05-05 35 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',但再读一遍可能是你是对的,所以回答更新。谢谢 –

相关问题