批处理文件 IF GEQ 尽管不匹配,但仍触发



我正在尝试在特定时间范围内启动和.exe,但尽管尚未匹配 GEQ(例如 20:55(,它仍然转到 :run 标签。

我也只尝试了 EQU,但在这里,尽管匹配了时间,但它并没有 :run。只有当我在确切的%时间%启动批处理时,它才有效,但显然这错过了重点。

这是怎么回事?

@ECHO OFF
SET hour=%time:~0,5%
echo It is %hour%
:check
echo.
echo %time:~0,5%
echo checking
timeout /t 60
IF %hour% GEQ 21:00 IF %hour% LEQ 22:00 (goto :run) else (goto :check)
:run
echo running
start chrome.exe
pause

也许你可以尝试这样的事情:

@echo off
:get_the_time
for /f %%# in ('wMIC Path Win32_LocalTime Get /Format:value') do @for /f %%@ in ("%%#") do @set %%@
echo CURRENT TIME -- %hour%:%minute%
:: creatring a comparable number with wich time for starting chrome can be used
if %hour% LSS 10 ( set hour=10%hour% ) else ( set hour=1%hour%)
if %minute% LSS 10 (set minute=0%minute%)
set comparable_time=%hour%%minute%
::now the comparable_time is in format 1HourMinute . The 1 in the front is to avoid complications with leading zero
timeout /t 60
if %comparable_time% GEQ 12100 if %comparable_time% LEQ 12200 ( goto :run ) else ( goto :get_the_time )
:run
echo running
start chrome.exe
pause

由于您获取时间的方法不是那么强大,并且取决于控制面板中的时间设置,因此我更喜欢使用 WMIC。

当您要与 IF 比较的内容中有非数字符号时,它将按字母顺序进行比较,因此:现在不是 IF 子句的一部分。

此外,还与

看起来像 1 小时分钟的变量进行比较,小时和分钟保持两位数格式,因此现在只使用与没有前导零的数字进行比较。

在PowerShell中处理日期很容易。在同一目录中创建两个文件。

=== runitat.ps1

$h = (Get-Date).Hour
if (($h -ge 20) -and ($h -le 21)) {
    Invoke-Command -Command {& "C:Program Files (x86)GoogleChromeApplicationchrome.exe"}
}

=== 符文.bat

:head
powershell -NoLogo -NoProfile -File %~dp0runitat.ps1
timeout /T 60
GOTO head

最新更新