We help IT Professionals succeed at work.

# Batch File: Easy math

on
Hi there,

I have an easy one for you to because I'm to ruched to deal with it right now.

Because I don't have the head nor the time to deal with this further, but need it asap.

I need to make a batch file to calculate the arrival time.

Rene
``````@ECHO OFF

SET SpeedInKM=120
SET KMtoGo=157

FOR /F "skip=1 tokens=1 3" %%A IN ('WMIC Path Win32_LocalTime Get Hour^,Minute ^| FINDSTR /R [0123456789]') DO SET CurrentTime=%%A

SET /a ArrivalTime=The math

ECHO Arrival Time is: %ArrivalTime%
ECHO.
PAUSE
EXIT
``````
Comment
Watch Question

## View Solution Only

Top Expert 2016

Commented:
Is the speed KM per Hour?

~bp

Commented:
Oh man! Thanks!!!
Yep, in KMs.

Commented:
Top Expert 2016

Commented:
Where would I go?

~bp

Commented:
157km further from wherever you are at 120 km/h. No stop signs, not red lights, not trafic... As if you were in a perfect streight line without anything slowing you down... ;)

Thanks
Top Expert 2016

Commented:

~bp

Commented:
You funny guy ;)
Top Expert 2016

Commented:
How do you want to handle the case where the trip crosses midnight?

So the times could come up as:

Now = 23:00
End = 01:00

Would that be okay for a 2 hour trip.

~bp

Commented:
You got it right!!
Top Expert 2016

Commented:
See what you think of this approach:

``````@echo off
setlocal

set SpeedInKM=120
set KMtoGo=157

for /F "tokens=1,2" %%A in ('WMIC Path Win32_LocalTime Get Hour^,Minute ^| FINDSTR /R [0123456789]') do (
set EndHour=%%A
set EndMinute=%%B
)
call :DisplayTime "Start Time" %EndHour% %EndMinute%

set /A Hours=KMtoGo / SpeedInKM
set /A Minutes=(60 * (KMtoGo - (Hours * SpeedInKM))) / SpeedInKM
call :DisplayTime "Elapsed time" %Hours% %Minutes%

set /A EndHour=(EndHour + Hours + (Minutes / 60)) %% 24
set /A EndMinute=(EndMinute + Minutes) %% 60
call :DisplayTime "Arrive Time" %EndHour% %EndMinute%

pause
exit /b

set "_h=0%~2" & set "_m=0%~3"
echo %~1 = %_h:~-2%:%_m:~-2%
exit /b
``````
~bp
Top Expert 2016
Commented:
Wee error there, corrected here...

``````@echo off
setlocal

set SpeedInKM=120
set KMtoGo=157

for /F "tokens=1,2" %%A in ('WMIC Path Win32_LocalTime Get Hour^,Minute ^| FINDSTR /R [0123456789]') do (
set EndHour=%%A
set EndMinute=%%B
)
call :DisplayTime "Start Time" %EndHour% %EndMinute%

set /A Hours=KMtoGo / SpeedInKM
set /A Minutes=(60 * (KMtoGo - (Hours * SpeedInKM))) / SpeedInKM
call :DisplayTime "Elapsed time" %Hours% %Minutes%

set /A EndHour=(EndHour + Hours + ((EndMinute + Minutes) / 60)) %% 24
set /A EndMinute=(EndMinute + Minutes) %% 60
call :DisplayTime "Arrive Time" %EndHour% %EndMinute%

pause
exit /b

set "_h=0%~2" & set "_m=0%~3"
echo %~1 = %_h:~-2%:%_m:~-2%
exit /b
``````
~bp

Commented:
Thanks Bill! You nailed it!
Verrrrrry appreciated..!!!

Cheers,
Rene
Top Expert 2016

Commented:
Welcome Rene.  Numbers are my friends...

~bp

Commented:
>> Numbers are my friends...

Can't be, because the actual answer is: 1hr, 18min AND 30sec... You're out by half a minute!!

That's the difference between landing on the moon and spiralling out of control towards the outer depth of the universe!!

(I wonder if there are any NASA scientists out there who would like to engage our batch file writing services....) :)
Top Expert 2016

Commented:
@paultomasi

I took my cue from Rene who only provided hours and minutes in the starting time, so worked a solution that stuck to no lower a level of precision.  I'm assuming this would drive something like an answer to the question "So, when will you get here?", and typically I don't answer those in seconds ;-).

~bp

Commented:
Hey Paul, you'r funny follow !!
Hmmm... maybe I should replan my vacations then ;)

By the way, it does not show the seconds for me.

Cheers

Commented:
@Bill
You figured it out right. It would have been educative, but I did not want to have the seconds. This is because it would require a bit more time to script and I needed it rush.

Thanks again pal!
Top Expert 2016

Commented:
@ReneGe

Do keep in mind also that all math in CMD is integer based and truncates, no rounding.  So for example

set /A X=2/3

evaluates to 0.  For your needs this is likely okay, but keep in mind answers will not be exact.

~bp

Commented:
@billprew:
Hmmm... So if I need properly round up, I would need to mathematically manipulate the numbers.
Like you sed, in my scenario, half a minute difference will not send me outer-space ;)

Cheers
Top Expert 2016

Commented:
Correct.

~bp

Commented:
@Bill:
Just for the fun and educative part of it, if I create a new question, do you feel like adding the seconds?
Top Expert 2016

Commented:
I can, but it won't be until tonight, out for some errands a bit today.  If you want to submit it later today I'll work it this evening...

~bp

Commented:
Ok, I'll send it later.
Thanks
Top Expert 2016

Commented:
I'm gonna poke at it now...

~bp