0

I'm trying to start and .exe at a certain time frame, but despite not matching GEQ yet (e.g. 20:55), it still goes to the :run label.

I've also tried only EQU, but here, despite matching the time, it doesn't goto :run. Only if i start the batch at the exact %time%, it works, but obviously that's missing the whole point.

Whats wrong here?

@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
Teh
  • 1
  • 1
    That is not a number. You only can do maths on integers (ie whole numbers in range of - 2 billion to + 2 billion). See `set /?` (at other times `for /?` may be better but not here) on how to chop up strings. – Noodles Apr 24 '19 at 20:00
  • Replace `IF %hour% GEQ 21:00 IF %hour% LEQ 22:00 (goto :run) else (goto :check)` with `IF %hour% GEQ 21:00 IF %hour% LEQ 22:00 (goto :run)` and a new line `goto :check` (and maybe add another `timeout` before) – Stephan Apr 24 '19 at 20:33
  • 1
    Although you are not comparing numbers but strings (due to the `:`), the comparison works as you have got fixed-length strings. However, execution continues at the next line when `IF %hour% GEQ 21:00` evaluates false. You could however add another `else` clause, like this: `IF %hour% GEQ 21:00 (IF %hour% LEQ 22:00 (goto :run) else (goto :check)) else (goto :check)`. Anyway, note that `%time%` returns the current time in a locale-dependent manner, so this works only in case you use 24h-time format (not AM/PM)... – aschipfl Apr 24 '19 at 22:22

2 Answers2

0

May be you can try something like this:

@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

As your method of getting time is not so robust and depends on the time settings in the control panel I preferred using the WMIC.

When there are non numerical symbols in the things you want to compare with IF it will make an alphabetical comparison so the : is not part of the IF clauses now.

Also a comparison is made with variable looking like 1Hourminute and the hour and minutes are kept in two digits format so now only a comparisons with numbers without leading zeros are used.

npocmaka
  • 51,748
  • 17
  • 123
  • 166
0

It is easy enough to handle dates in PowerShell. Create two files in the same directory.

=== 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
lit
  • 10,936
  • 7
  • 49
  • 80