View Full Version : set /a and vars


winter
02-24-2004, 09:22 PM
Hi All,

im trying to do a quick little time diff script.
note: ill work out all the quirks leading 0 if AM, etc later....

set sms_time=%time:~0,8%
reg add HKLM\Software\Winter /v last_sms /t REG_SZ /d %sms_time%

::Short time later.....

req query HKLM\Software\Winter /v last_sms >%temp%\yada
for /f "skip=3 tokens=3" %a in (%temp%\yada) do set sms_sent=%a
set mytime=%time:~0,8%
set /a diff=%mytime%-%sms_sent%

I tried very unsucessfully to play with enabledelayedexpansion, and
replacing the % in the set /a cmd to ! - but im sure that im (a) not
understanding enabledelayedexpansion & (b) using it wrong as a result
of (a)


thanx guys n gals!

w

Phil Robyn [MVP]
02-24-2004, 10:40 PM
winter wrote:

> Hi All,
>
> im trying to do a quick little time diff script.
> note: ill work out all the quirks leading 0 if AM, etc later....
>
> set sms_time=%time:~0,8%
> reg add HKLM\Software\Winter /v last_sms /t REG_SZ /d %sms_time%
>
> ::Short time later.....
>
> req query HKLM\Software\Winter /v last_sms >%temp%\yada
> for /f "skip=3 tokens=3" %a in (%temp%\yada) do set sms_sent=%a
> set mytime=%time:~0,8%
> set /a diff=%mytime%-%sms_sent%
>
> I tried very unsucessfully to play with enabledelayedexpansion, and
> replacing the % in the set /a cmd to ! - but im sure that im (a) not
> understanding enabledelayedexpansion & (b) using it wrong as a result
> of (a)
>
>
> thanx guys n gals!
>
> w

Your main problem is trying to do math ('set /a') on values of the
format 'hh:mm:ss'. A string like '14:34:52' is *not* a number, so
'set /a' cannot be used.

Why do you want to put the 'sms_time' in a REGISTRY entry????

Maybe the following demo will give you some ideas:

- - - - - - - - - - begin screen capture Win2000 - - - - - - - - - -
C:\cmd>demo\CalcTimeDiff 5
time1 is 52291
time2 is 52296
timediff is 5

C:\cmd>demo\CalcTimeDiff 19
time1 is 52300
time2 is 52319
timediff is 19

C:\cmd>rlist demo\CalcTimeDiff.cmd
=====begin C:\cmd\demo\CalcTimeDiff.cmd ====================
01. @echo off
02. setlocal enabledelayedexpansion
03. call :gettime time1
04. sleep %1
05. call :gettime time2
06. call :timediff time1 time2
07. echo timediff is %timediff%
08. goto :EOF
09. :gettime
10. set mytime=%time:~0,8%
11. set mytime=%mytime: =0%
12. for /f "tokens=1-3 delims=:. " %%a in ("%mytime%") do (
13. set /a hh = 1%%a - 100
14. set /a mm = 1%%b - 100
15. set /a ss = 1%%c - 100
16. )
17. set /a %1 = hh * 3600
18. set /a %1 += mm * 60
19. set /a %1 += ss
20. echo/%1 is !%1!
21. goto :EOF
22. :timediff
23. set /a timediff = %2 - %1
24. goto :EOF
=====end C:\cmd\demo\CalcTimeDiff.cmd ====================
- - - - - - - - - - end screen capture Win2000 - - - - - - - - - -

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

Phil Robyn [MVP]
02-24-2004, 10:40 PM
winter wrote:

> Hi All,
>
> im trying to do a quick little time diff script.
> note: ill work out all the quirks leading 0 if AM, etc later....
>
> set sms_time=%time:~0,8%
> reg add HKLM\Software\Winter /v last_sms /t REG_SZ /d %sms_time%
>
> ::Short time later.....
>
> req query HKLM\Software\Winter /v last_sms >%temp%\yada
> for /f "skip=3 tokens=3" %a in (%temp%\yada) do set sms_sent=%a
> set mytime=%time:~0,8%
> set /a diff=%mytime%-%sms_sent%
>
> I tried very unsucessfully to play with enabledelayedexpansion, and
> replacing the % in the set /a cmd to ! - but im sure that im (a) not
> understanding enabledelayedexpansion & (b) using it wrong as a result
> of (a)
>
>
> thanx guys n gals!
>
> w

Your main problem is trying to do math ('set /a') on values of the
format 'hh:mm:ss'. A string like '14:34:52' is *not* a number, so
'set /a' cannot be used.

Why do you want to put the 'sms_time' in a REGISTRY entry????

Maybe the following demo will give you some ideas:

- - - - - - - - - - begin screen capture Win2000 - - - - - - - - - -
C:\cmd>demo\CalcTimeDiff 5
time1 is 52291
time2 is 52296
timediff is 5

C:\cmd>demo\CalcTimeDiff 19
time1 is 52300
time2 is 52319
timediff is 19

C:\cmd>rlist demo\CalcTimeDiff.cmd
=====begin C:\cmd\demo\CalcTimeDiff.cmd ====================
01. @echo off
02. setlocal enabledelayedexpansion
03. call :gettime time1
04. sleep %1
05. call :gettime time2
06. call :timediff time1 time2
07. echo timediff is %timediff%
08. goto :EOF
09. :gettime
10. set mytime=%time:~0,8%
11. set mytime=%mytime: =0%
12. for /f "tokens=1-3 delims=:. " %%a in ("%mytime%") do (
13. set /a hh = 1%%a - 100
14. set /a mm = 1%%b - 100
15. set /a ss = 1%%c - 100
16. )
17. set /a %1 = hh * 3600
18. set /a %1 += mm * 60
19. set /a %1 += ss
20. echo/%1 is !%1!
21. goto :EOF
22. :timediff
23. set /a timediff = %2 - %1
24. goto :EOF
=====end C:\cmd\demo\CalcTimeDiff.cmd ====================
- - - - - - - - - - end screen capture Win2000 - - - - - - - - - -

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l