Created
August 28, 2014 14:37
-
-
Save jcefoli/57881d79aa4c7548324e to your computer and use it in GitHub Desktop.
This snippet calculates a start time, end time and total duration for your batch script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ECHO off | |
SET STARTTIME=%TIME% | |
REM DO THINGS HERE TO TIME | |
timeout 5 | |
REM DO THINGS HERE TO TIME | |
REM Final Calculations | |
SET ENDTIME=%TIME% | |
FOR /F "tokens=1-4 delims=:.," %%a IN ("%STARTTIME%") DO ( | |
SET /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" | |
) | |
FOR /F "tokens=1-4 delims=:.," %%a IN ("%ENDTIME%") DO ( | |
SET /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" | |
) | |
REM Calculate the elapsed time by subtracting values | |
SET /A elapsed=end-start | |
REM Format the results for output | |
SET /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100 | |
IF %hh% lss 10 SET hh=0%hh% | |
IF %mm% lss 10 SET mm=0%mm% | |
IF %ss% lss 10 SET ss=0%ss% | |
IF %cc% lss 10 SET cc=0%cc% | |
SET DURATION=%hh%:%mm%:%ss%,%cc% | |
ECHO Start : %STARTTIME% | |
ECHO Finish : %ENDTIME% | |
ECHO --------------- | |
ECHO Duration : %DURATION% | |
PAUSE | |
REM Based on http://stackoverflow.com/questions/9922498/calculate-time-difference-in-batch-file |
First of all you call into your subroutine ":TrimS", which has no return. I would have expected to see a "goto :EOF" at the end of it, and not an exit /b. Your script sounds like it just simply exits right after starting.
Has this script ever worked for you before even modifying the for loop I mentioned?
How well do you understand BATCH language and the various control structures?
Hi gweber68,
Thank for your answer!
- Since a lot of time I use this subroutine BUT If you remove the call :TrimS the result it's same
- If you insert a 'pause' before and after your FOR command, You can easy verify that is this command with problem
- The batch of the first post run fine, the batch with your FOR coomand for AM/PM management ever worked !
Regards
It's 2024. Why are we not using PowerShell? This is fugly
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi gweber68,
I try your suggestion about AM/PM with this code but without success... :(
`ECHO off
Set endtime=23:59:57,07 &:: !!!!!!!!!
SET STARTTIME=%endtime%
call :TrimS STARTTIME %STARTTIME%
REM Final Calculations
SET ENDTIME=%TIME%
ECHO StartTime: -%STARTTIME%-
ECHO EndTime: -%EndTime%-
pause
FOR /F "tokens=1-4 delims=:.," %%a IN ("%STARTTIME%") DO (
SET /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
for /F "tokens=1-4 delims=:.," %%a in ("%ENDTIME%") do (
IF %ENDTIME% GTR %STARTTIME% set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
IF %ENDTIME% LSS %STARTTIME% set /A "end=((((%%a+24)*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100")
REM Calculate the elapsed time by subtracting values
SET /A elapsed=end-start
REM Format the results for output
SET /A hh=elapsed/(6060100), rest=elapsed%%(6060100), mm=rest/(60100), rest%%=60100, ss=rest/100, cc=rest%%100
IF %hh% lss 10 SET hh=0%hh%
IF %mm% lss 10 SET mm=0%mm%
IF %ss% lss 10 SET ss=0%ss%
IF %cc% lss 10 SET cc=0%cc%
SET DURATION=%hh%:%mm%:%ss%,%cc%
ECHO StartTime : %STARTTIME%
ECHO EndTime : %ENDTIME%
ECHO ---------------
ECHO Duration : %DURATION%
PAUSE
exit
:TrimS
SetLocal EnableDelayedExpansion
set Params=%*
for /f "tokens=1*" %%a in ("!Params!") do EndLocal & set %1=%%b
exit /b`
Where is the error ?
Thanks very much !
Luciano