We help IT Professionals succeed at work.

Batch File: Easy math

ReneGe
ReneGe asked
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.

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

Comment
Watch Question

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Is the speed KM per Hour?

~bp

Author

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

Author

Commented:
So glad you'r still arround!!
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Where would I go?

~bp

Author

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
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
My question was referring to your "So glad you'r still arround!!"

~bp

Author

Commented:
You funny guy ;)
Bill PrewTest your restores, not your backups...
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

Author

Commented:
You got it right!!
Bill PrewTest your restores, not your backups...
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

: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
Test your restores, not your backups...
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

: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

Author

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

Cheers,
Rene
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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....) :)
Bill PrewTest your restores, not your backups...
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

Author

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

Author

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!
Bill PrewTest your restores, not your backups...
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

Author

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
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Correct.

~bp

Author

Commented:
@Bill:
Just for the fun and educative part of it, if I create a new question, do you feel like adding the seconds?
Bill PrewTest your restores, not your backups...
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

Author

Commented:
Ok, I'll send it later.
Thanks
Bill PrewTest your restores, not your backups...
Top Expert 2016

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

~bp
Bill PrewTest your restores, not your backups...
Top Expert 2016

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