Troy Rash
Troy Rash

Reputation: 29

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.

Upvotes: 0

Views: 163

Answers (2)

Endoro
Endoro

Reputation: 37589

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!

Upvotes: 2

Magoo
Magoo

Reputation: 80123

@ECHO OFF
SETLOCAL
SET testvalues=" 1" " 4" " 5" " 6" "10" "12" "13" "19" "20" "23" "01" "04" "05" "06" "08" " 0" 
FOR %%i IN (%testvalues%) DO CALL :test2 %%i
echo==================================
FOR %%i IN (%testvalues%) DO CALL :test %%i
GOTO :eof
:test
SET value=%~1
SET "HH="
FOR /f "delims=0" %%a IN ("%value:~0,2%") DO SET /a HH=%%a
IF %HH% GTR 5 (ECHO Alert! FOR %1 ) ELSE (ECHO Silent FOR %1 )

GOTO :EOF
:test2
SET value=%~1
SET "HH="

SET /a HH=1%value: =0%
IF %HH% GTR 105 (ECHO Alert! FOR %1 ) ELSE (ECHO Silent FOR %1 )

GOTO :EOF

Results:

Silent FOR " 1" 
Silent FOR " 4" 
Silent FOR " 5" 
Alert! FOR " 6" 
Alert! FOR "10" 
Alert! FOR "12" 
Alert! FOR "13" 
Alert! FOR "19" 
Alert! FOR "20" 
Alert! FOR "23" 
Silent FOR "01" 
Silent FOR "04" 
Silent FOR "05" 
Alert! FOR "06" 
Alert! FOR "08" 
Silent FOR " 0" 
=================================
Silent FOR " 1" 
Silent FOR " 4" 
Silent FOR " 5" 
Alert! FOR " 6" 
Silent FOR "10" 
Alert! FOR "12" 
Alert! FOR "13" 
Alert! FOR "19" 
Silent FOR "20" 
Alert! FOR "23" 
Silent FOR "01" 
Silent FOR "04" 
Silent FOR "05" 
Alert! FOR "06" 
Alert! FOR "08" 
Missing operand.
5 was unexpected at this time.

Note : corrected behaviour for hours=10, 20 and 0

Upvotes: 2

Related Questions