目前我正在运行多个批处理文件并行调用主批处理文件。我想计算完成所有批处理文件的执行所需的时间。
例如,我有 MainScript.bat 文件,它同时调用了 20 个以上的批处理文件。现在我想知道完成执行脚本需要多少时间。
目前我正在运行多个批处理文件并行调用主批处理文件。我想计算完成所有批处理文件的执行所需的时间。
例如,我有 MainScript.bat 文件,它同时调用了 20 个以上的批处理文件。现在我想知道完成执行脚本需要多少时间。
感谢 foxidrive 所做的工作。我需要一个简单的计时器,我可以调用它来显示经过的秒数。这是修改后的代码:
@echo off
set t1=%time%
:loop
call :print-elapsed-sec
PING -n 2 127.0.0.1>nul
goto :loop
:print-elapsed-sec
setlocal
set t2=%time%
for /f "tokens=1-4 delims=:." %%d in ("%t1%") do set /a "t1=%%d*360000+1%%e*6000+1%%f*100+1%%g-610100"
for /f "tokens=1-4 delims=:." %%d in ("%t2%") do set /a "t2=%%d*360000+1%%e*6000+1%%f*100+1%%g-610100, t=t2-t1"
echo.
set /a "sec=t/100"
echo %~1%sec% sec
exit /b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Timer ID
::
:: By: Ritchie Lawrence, 2002-10-10. Version 1.0
::
:: Func: Returns number of seconds elapsed since the function was last
:: called and first called. For NT4/2K/XP.
::
:: Args: %1 (by ref) The first time this function is called, this variable
:: is initialised to '<last> <first> <init>' where <first> and <last>
:: are zero and <init> is the number of elapsed seconds since
:: 1970-01-01 00:00:00. This value is used by subsequent calls to
:: determine the elapsed number of seconds since the last call
:: (<last>) and the first call (<first>).
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS&call set ID=%%%1%%
set t=2&if "%date%z" LSS "A" set t=1
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
set %%a=%%d&set %%b=%%e&set %%c=%%f))
for /f "tokens=5-7 delims=:. " %%a in ('echo/^|time') do (
set hh=%%a&set nn=%%b&set ss=%%c)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
set /a j=j*86400+hh*3600+nn*60+ss
for /f "tokens=1-3 delims= " %%a in ('echo/%ID%') do (
set l=%%a&set f=%%b&set c=%%c)
if {%c%}=={} endlocal&set %1=0 0 %j%&goto :EOF
set /a l=j-c-l,f+=l
endlocal&set %1=%l% %f% %c%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
一个更简单的计时器工具:
像这样启动它: timer.bat ping localhost
@echo off
set t1=%time%
call %*
set t2=%time%
call :showTime "Time taken:" "%t1%" "%t2%"
goto :EOF
:showTime
setlocal
for /f "tokens=1-4 delims=:." %%d in (%2) do set /a "t1=%%d*360000+1%%e*6000+1%%f*100+1%%g-610100"
for /f "tokens=1-4 delims=:." %%d in (%3) do set /a "t2=%%d*360000+1%%e*6000+1%%f*100+1%%g-610100, t=t2-t1"
echo.
echo %~1 %t%0 msec
pause
goto :EOF
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do set %x
将设置变量Day
, DayofWeek
, Hour
, Minute
, Month
, Quarter
, Second
,WeekInMonth
以及Year
可用于计算的变量。