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

时间:2019-04-24 19:57:00

标签: windows batch-file cmd

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

我也只尝试了EQU,但是在这里,尽管匹配了时间,但它并没有转到:run。仅当我以准确的%time%开始批处理时,它才能工作,但很明显,这遗漏了整个要点。

这是怎么了?

@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

2 个答案:

答案 0 :(得分:0)

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

@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 Hour minute 的变量进行比较,并且小时和分钟以两位数字格式保存,因此现在只进行数字比较而不带前导零使用。

答案 1 :(得分:0)

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

=== runitat.ps1

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

=== runitat.bat

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