How to solve Time in loop context in CMD -


i did own code try calculate delay when execute program within loop. weakness value %time% evaluated @ same moment. want ask you, possible change code move 2 blocks of calculations (using set /a) separate subroutines. searched web solution goto, not clear me how return loop. examples have found, referred on end of file.

@echo off  setlocal enabledelayedexpansion  /f "tokens=*" %%a in ('date/t') %%b in (%%a) set today=%%b echo it's %today% today  chcp 1250 > nul  set tpath=temp  /f "delims=" %%x in (delimiter.ini) set tab=%%x  if not exist proxies.ini (   echo proxies not found!   pause  exit )  rem set array of proxies /f "eol= tokens=1,2 delims=%tab%" %%a in (proxies.ini) (   if "%%a"=="*" (     set asterisk=1     set http_proxy=%%b     set t0=%time%     wget.exe http://www.nasa.gov/images/content/297522main_image_1244_946-710.jpg      set t1=%time%     rem /f %%a in ('time/t') set t1=%%a      echo !t0!     set /a s = !t0:~6,2!*100     set /a c = !t0:~9,2!     set /a c = "!c!-(!c!/10)*4"     set /a d1 = !s!+!c!     echo !s!+!c! = !d1!     echo !d1!      echo !t1!     set /a s = !t1:~6,2!*100     set /a c = !t1:~9,2!     set /a c = "!c!-(!c!/10)*4"     set /a d2 = !s!+!c!     echo !s!+!c! = !d2!     set /a delay = !d1!-!d2!        echo delay:!delay!     pause    echo working proxy ... !proxy!        ) )   pause 

edit second test, before dbenham posted more better solution:

@echo off  setlocal enabledelayedexpansion  /f "tokens=*" %%a in ('date/t') %%b in (%%a) set today=%%b echo it's %today% today  chcp 1250 > nul  set tpath=temp  /f "delims=" %%x in (delimiter.ini) set tab=%%x  if not exist proxies.ini (   echo proxies not found.    pause  exit )  rem set array of proxies /f "eol= tokens=1,2 delims=%tab%" %%a in (proxies.ini) (   if "%%a"=="*" (     set asterisk=1     set http_proxy=%%b     set t0=!time!     wget.exe http://www.nasa.gov/images/content/297522main_image_1244_946-710.jpg      call :settime !t0! !time!     set t1=!time!  :settime    setlocal enabledelayedexpansion     set t0=%1     set t1=%2     echo !t0! !t1!     pause     set /a m1 = !t0:~3,2!     set /a s1 = !t0:~6,2!     set /a s = !s1!*100     set /a c = !t0:~9,2!     set /a c = "!c!-((!c!/10)*4)"     set /a d1 = !s!+!c!     echo !s!+!c! = !d1!     echo !d1!      echo !t1!     set /a m2 = !t1:~3,2!     set /a s2 = !t1:~6,2!     if "!m2!" gtr "!m1!" (       if "!s2!" lss "!s1!" (           rem e.g. s1=20, s2=04 =>            set /a s2_cor = 60-!s1!+!s2!           echo correction: s2: !s2!           set /a s2_cor = "!s2!+60*(!m2!-!m1!)"        ) else (           set /a s2_cor = "!s2!+60*(!m2!-!m1!-1)"       )        set /a s2 = "!s2!+!s2_cor!"     )      set /a s = !s2!*100         echo s2:!s2! , s:!s!         set /a c = !t1:~9,2!     set /a c = "!c!-((!c!/10)*4)"     set /a d2 = !s!+!c!     echo !s!+!c! = !d2!     set /a delay = !d2!-!d1!        echo delay:!delay!    pause    endlocal exit /b     echo working proxy ... !proxy!        ) )  if not !asterisk!==1 (  echo .  echo asterix not set  echo .  exit ) pause 

joey diagnosed 1 of problems. computation of elapsed time has many problems.

  • as joey pointed out, need use delayed expansion within loop

  • you should subtract time1 time2. doing opposite.

  • your handling of fractional seconds wrong.

  • you assume elapsed time less 1 minute. may or may not true. erroneously assume need @ seconds portion of time. if t0=09:59:59.99 , t1=10:00:00.00? correct result should 0.01 seconds. way correct result @ of time components. simplest way compute value convert each time number of centiseconds (1/100 second) past midnight prior doing substraction.

  • what if t0=23:59:59.99 , t1=00:00:00.00? correct result should again 0.01 seconds. if convert both values centiseconds , subtract t0 t1 negative number. trick add 1 day (24*60*60*100) result if result negative. allows technique compute time interval less 24 hours @ time of day.

  • set /a interprets number starts 0 octal notation. number greater 7 being interpreted incorrectly in code. plus values 08 , 09 raising errors because 8 , 9 not valid octal digits. solution below uses math tricks add 1 digit before each time component force set /a use decimal notation, , subtracts out time @ end.

other issues:

  • most of time when using set /a not need expand variables. example, if want add 1 numeric variable, can use set /a var=var+1, or more concisely set var+=1.

  • i don't think /f options "eol= tokens=1,2 delims=%tab%" doing want. suspect attempting disable eol option, in reality setting eol <space> character. proper way disable set eol 1 of delim values: "eol=%tab% tokens=1,2 delims=%tab%".

here relevent section of code using working batch subroutines compute elapsed time. algorithms used dealing time computations derived code found here: http://www.dostips.com/dtcodefunctions.php#_toc128586395

there lot of advanced batch techniques in code below. don't feel bad if of doesn't make sense. should able copy subroutines script want , compute elapsed time ease.

setlocal enabledelayedexpansion /f "eol= tokens=1,2 delims=%tab%" %%a in (proxies.ini) (   if "%%a"=="*" (     set asterisk=1     set http_proxy=%%b     set t0=!time!     wget.exe http://www.nasa.gov/images/content/297522main_image_1244_946-710.jpg     set t1=!time!      call :difftime t0 t1 delay     call :difftime /f t0 t1 formatteddelay     echo delay=!delay!     echo formatteddelay=!formatteddelay!      pause     echo working proxy ... !proxy!   ) ) exit /b  :difftime  [/f]  starttimevar  endtimevar  [returnvar] :: :: compute elapsed time between time values stored in :: variables starttimevar , endtimevar. value returned :: in units of centiseconds (1/100 second). if /f option :: specified result formatted hh:mm:ss.dd :: :: result returned in variable returnvar. :: :: if returnvar not specified echo result. ::   setlocal enabledelayedexpansion   set "difftime.formattime="   if /i "%~1"=="/f" (     set difftime.formattime=1     shift /1   )   call :parsetime %1 difftime.t1   call :parsetime %2 difftime.t2   set /a "diff=difftime.t2-difftime.t1"   if %diff% lss 0 set /a "diff+=24*60*60*100"   if defined difftime.formattime (     set /a "dd=diff, hh=dd/360000, dd-=hh*360000, mm=dd/6000, dd-=mm*6000, ss=dd/100, dd-=ss*100"%\n%     if "!hh:~1!"=="" set "hh=0!hh!"     if "!mm:~1!"=="" set "mm=0!mm!"     if "!ss:~1!"=="" set "ss=0!ss!"     if "!dd:~1!"=="" set "dd=0!dd!"     set diff=!hh!:!mm!:!ss!.!dd!   )   endlocal & if "%~3"=="" (echo %diff%) else set "%~3=%diff%" exit /b   :parsetime  timevar  returnvar :: :: parse time value stored in timevar , compute number of centiseconds (1/100 second) past midnight. :: result returned in variable returnvar ::   /f "tokens=1-4 delims=:.," %%a in ("!%~1: =0!") set /a "%~2=(((1%%a*60)+1%%b)*60+1%%c)*100+1%%d-36610100" exit /b 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -