Hello,
Anyone interested in creating a simple stopwatch in a batch file?
Thanks,
davep
challenge: a dos shell stopwatch
Moderator: DosItHelp
I am indeed trying, and I'm quite stuck.
Thank you,
davep
Code: Select all
@echo off & color 0e
:: based on system clock?
:: can run manually, or be called from another bat to time its process
:: make unique string x based on start time
:: make unique string y based on stop time
:: subtract x from y
:: expand back to standard time format with (:) and (.)?
setlocal enabledelayedexpansion
cls & title Stopwatch
:begin
set /p start=Press Enter to start the stopwatch:
for /f "tokens=1-7 delims=/-:. " %%A in ("%time =0%") do set /a "x=%%A%%B%%C%%D"
echo.%x%
set /p stop=Press Enter to stop the stopwatch:
for /f "tokens=1-7 delims=/-:. " %%A in ("%time =0%") do set /a "y=%%A%%B%%C%%D"
echo.%y%
set /a z=y-x
echo.%z%
echo. & pause
cls & goto begin
Thank you,
davep
solved
Got it!
Code: Select all
@echo off & color 0e
setlocal enabledelayedexpansion
cls & title Stopwatch
:begin
set /p start=Press Enter to start the stopwatch:
for /f "tokens=1-7 delims=/-:. " %%A in ("%time%") do set /a "x=%%A%%B%%C%%D"
echo.%x%
echo.
set /p stop=Press Enter to stop the stopwatch:
for /f "tokens=1-7 delims=/-:. " %%A in ("%time%") do set /a "y=%%A%%B%%C%%D"
echo.%y%
set /a z=y-x
echo.
:: split string & add decimal in echo
set z1=%z:~0,-2%
set z2=%z:~-2,2%
echo The difference is %z1%.%z2% seconds.
echo. & pause
cls & goto begin
Hi davep,
In my country the decimal point is a comma, thats not really a problem.
But the rest can't work.
Take the time at " 9:24:02,56" and " 9:25:02,56"
this should result in 60.0 seconds, but your code results in 100.0 seconds.
Next problem, try it with "0:28:24,56", you get an converting error,
because the cmd-processor try to convert "028..." as a number in octal format.
My code example
jeb
In my country the decimal point is a comma, thats not really a problem.
But the rest can't work.
Take the time at " 9:24:02,56" and " 9:25:02,56"
this should result in 60.0 seconds, but your code results in 100.0 seconds.
Next problem, try it with "0:28:24,56", you get an converting error,
because the cmd-processor try to convert "028..." as a number in octal format.
My code example
Code: Select all
@echo off
SETLOCAL
set start=%time%
for /L %%c in (1,1,4000) DO set something=%%c*3
set end=%time%
call :timeToMs ms_start "%start%"
call :timeToMs ms_end "%end%"
echo %start%=%ms_start%ms
echo %end%=%ms_end%ms
set /a diff_ms=ms_end-ms_start
echo diff=%diff_ms%ms
goto:eof
::::::::::::::::::.
:timeToMs <resultVar> <time>
SETLOCAL
FOR /F "tokens=1,2,3,4 delims=:,.^ " %%a IN ("%~2") DO (
set /a ms=^(^(^(30%%a%%100^)*60+7%%b^)*60+3%%c-42300^)*1000+^(1%%d0 %% 1000^)
)
(
ENDLOCAL
set %~1=%ms%
)
jeb
Jeb, you're right. My stopwatch is terribly flawed. I'm not sure how to fix it, though. Your code sample is way beyond my comprehension, and when I run it it just exits. I added a pause to the end and commented out "@echo off," and I still just don't understand what's going on there. What I'd like to achieve is "press enter to start the stopwatch, press enter to stop, and have a nice neat (and correct) difference displayed. I use 24-hour time, if that helps. Thank you!