Search code examples
batch-filecommanddostimed

Batch: I want to call a command after 5 am yet it outputs it less than 5 am no matter what


Neither

@echo off

set hh=!tm:~0,2
set mm=!tm:~3,2
set ss=!tm:~5,2
set ms=!tm:~7,2

if !hh! gtr **5** (
    echo gtr 5
    pause
    goto success
)


pause
goto fail

:success
echo Success!
pause
exit

:fail
echo Fail!
pause
exit

or

@echo off

set hh=!tm:~0,2
set mm=!tm:~3,2
set ss=!tm:~5,2
set ms=!tm:~7,2

if !hh! gtr **05** (
    echo gtr 5
    pause
    goto success
)


pause
goto fail

:success
echo Success!
pause
exit

:fail
echo Fail!
pause
exit

works as i need it to

This should get it to output that it is greater than 5 am when it is 10 am. It only states it is less than 5 am even though it is 10 am. On top of that if I set the hour to 5 am it still says it is less than 5 am and not equal to 5 am.

It only does this for anytime that is a single digit, so 0-9 (which is 12am to 9 am). Any time that is a double digit, so 10-23 (which is 10am to 11pm), works and says the correct things.


Solution

  • try this:

    @ECHO OFF &SETLOCAL
    set /a HH=0
    FOR /f "tokens=1*delims=0" %%a IN ("$0%time:~0,2%") DO SET /a HH=%%b 2>nul
    IF %HH% GTR 5 ECHO Alert!