challenge: a dos shell stopwatch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
davep
Posts: 24
Joined: 29 May 2008 14:03
Location: Nauf Kakalak

challenge: a dos shell stopwatch

#1 Post by davep » 12 Jun 2008 10:19

Hello,

Anyone interested in creating a simple stopwatch in a batch file?

Thanks,

davep

davep
Posts: 24
Joined: 29 May 2008 14:03
Location: Nauf Kakalak

#2 Post by davep » 12 Jun 2008 10:33

I am indeed trying, and I'm quite stuck.

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

davep
Posts: 24
Joined: 29 May 2008 14:03
Location: Nauf Kakalak

#3 Post by davep » 12 Jun 2008 10:46

OK, it seems to work now that I've changed ("%time =0%") to just ("%time%")

Now, how to get the decimal back in the string before the last two digits?

Thanks,

davep

davep
Posts: 24
Joined: 29 May 2008 14:03
Location: Nauf Kakalak

solved

#4 Post by davep » 13 Jun 2008 13:57

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#5 Post by jeb » 15 Jun 2008 00:18

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

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

davep
Posts: 24
Joined: 29 May 2008 14:03
Location: Nauf Kakalak

#6 Post by davep » 16 Jun 2008 07:10

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!

Post Reply