0

I have a batch file which is deployed to machines as soon as they're able to receive the file (which is a variable - as some may be offline, busy, or delayed), but it should only run it the current local time is inside a specified window.

Eg, only between 12am & 2am.

I do have the following working with PM times- But apparently it will not execute if I specify any single (or double) digit AM hours here (such as 1am through to 9am).

@echo off
SET hour=%time:~0,2%
SET shouldrun=True

IF %hour% leq 23 SET shouldrun=False
IF %hour% geq 02 SET shouldrun=False

IF "%shouldrun%"=="False" (
        echo Outside Update Schedule
        EXIT /B 1
)

IF "%shouldrun%"=="True" (
        @TASKKILL /f /im some.exe > nul 2>&1
        @timeout /t 4 > nul
         - do things here -
        @timeout /t 2 > nul
        shutdown -r -f -y -t 2
        EXIT /B 0
)
4

3 回答 3

1

这似乎工作正常:

@echo off
SET hour=%time:~0,2%
SET shouldrun=True

IF %hour% GEQ 02 IF %hour% LEQ 19 SET shouldrun=False

IF "%shouldrun%"=="False" (
        echo Outside Update Schedule
        EXIT /B 1
)

IF "%shouldrun%"=="True" (
        @TASKKILL /f /im some.exe > nul 2>&1
        @timeout /t 4 > nul
         - do things here -
        @timeout /t 2 > nul
        shutdown -r -f -y -t 2
        EXIT /B 0
)
于 2021-07-16T22:45:43.700 回答
0

试试这样:

@echo off & setlocal  ENABLEDELAYEDEXPANSION

::takes the current hour independent of the time format
for /f "usebackq tokens=* delims=" %%i in (`wmic os get LocalDateTime /value`) do for /f "tokens=* delims=" %%# in ("%%i") do set "%%#"
set "hour=%LocalDateTime:~6,2%"

::removes the leading zeroes
cmd /c exit /b %hour%
set hour=%errorlevel%

::checks if should run
if !hour! LEQ 23 if !hour! GEQ 2  (
        echo Outside Update Schedule
        EXIT /B 1
)

echo ---RUNNING----
::your code here. Second if is not needed.
于 2021-07-13T14:19:54.757 回答
0

如果我要为此考虑,那么我会使用不同的类来做,这样我就不会遇到前导零的问题,也不需要 for 循环。

@%SystemRoot%\System32\wbem\WMIC.exe Path Win32_LocalTime Where "Hour < 2" | %SystemRoot%\System32\findstr.exe "\<0\> \<1\>" 1>NUL 2>&1 || Echo Exit /B 1
@Rem Your line(s) here
于 2021-07-13T19:07:03.400 回答