Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

Batch File: Easy math

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.

Thanks for your precious help,
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

Open in new window

Avatar of Bill Prew
Bill Prew

Is the speed KM per Hour?

~bp
Avatar of ReneGe

ASKER

Oh man! Thanks!!!
Yep, in KMs.
Avatar of ReneGe

ASKER

So glad you'r still arround!!
Where would I go?

~bp
Avatar of ReneGe

ASKER

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
My question was referring to your "So glad you'r still arround!!"

~bp
Avatar of ReneGe

ASKER

You funny guy ;)
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
Avatar of ReneGe

ASKER

You got it right!!
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

:DisplayTime [heading] [hour] [minute]
  set "_h=0%~2" & set "_m=0%~3"
  echo %~1 = %_h:~-2%:%_m:~-2%
  exit /b

Open in new window

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

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

Cheers,
Rene
Welcome Rene.  Numbers are my friends...

~bp
>> 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....) :)
@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
Avatar of ReneGe

ASKER

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
Avatar of ReneGe

ASKER

@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!
@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
Avatar of ReneGe

ASKER

@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
Correct.

~bp
Avatar of ReneGe

ASKER

@Bill:
Just for the fun and educative part of it, if I create a new question, do you feel like adding the seconds?
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
Avatar of ReneGe

ASKER

Ok, I'll send it later.
Thanks
I'm gonna poke at it now...

~bp
Start Time = 15:51:49
Elapsed time = 01:18:30
Arrive Time = 17:10:19