【发布时间】:2015-02-15 02:19:19
【问题描述】:
如何使用这样的脚本来记录批处理脚本中 for 循环的每次迭代所花费的时间?我已经实现了这个来记录整个脚本的执行时间,但我想获取 for 循环的每次迭代的持续时间。
@echo off
rem ****************** MAIN CODE SECTION
set STARTTIME=%TIME%
rem Your code goes here (remove the ping line)
ping -n 4 -w 1 127.0.0.1 >NUL
set ENDTIME=%TIME%
rem ****************** END MAIN CODE SECTION
rem Change formatting for the start and end times
for /F "tokens=1-4 delims=:.," %%a in ("%STARTTIME%") do (
set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
for /F "tokens=1-4 delims=:.," %%a in ("%ENDTIME%") do (
set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
rem Calculate the elapsed time by subtracting values
set /A elapsed=end-start
rem Format the results for output
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%
set DURATION=%hh%:%mm%:%ss%,%cc%
echo Start : %STARTTIME%
echo Finish : %ENDTIME%
echo ---------------
echo Duration : %DURATION%
输出:
Start : 11:02:45.92
Finish : 11:02:48.98
---------------
Duration : 00:00:03,06
编辑:这就是我的实现的样子
@ECHO on
setlocal enabledelayedexpansion
cls
RMDIR c:\path1 /s /q
mkdir c:\path1
xcopy "\\path2\*" c:\path1 /s /i
cd c:\path3
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
cd c:\path1
sqlcmd -S server -i "\\path2\DropDatabases.sql"
for /D /r %%F IN ("*") DO (
for %%G IN ("%%F\*.extenstion1") DO xcopy "%%G" c:\path2\%newest% /y /i
for /l %%A in (1,1,5) do (
set STARTTIME=!TIME!
for /f "delims=_" %%J IN ('forfiles /p "%%F" /m *.jmpt /c "cmd /c echo @path"') DO start "program" /D "c:\path3" /Wait program -r %%J
set ENDTIME=!TIME!
call :GetDuration !STARTTIME! !ENDTIME!
)
)
exit /b
:GetDuration
set function_starttime=%1
set function_endtime=%2
rem Change formatting for the start and end times
for /F "tokens=1-4 delims=:.," %%a in ("%function_starttime%") do (
set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
for /F "tokens=1-4 delims=:.," %%a in ("%function_endtime%") do (
set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
rem Calculate the elapsed time by subtracting values
set /A elapsed=end-start
rem Format the results for output
set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
if %hh% lss 10 set hh=0%hh%
if %mm% lss 10 set mm=0%mm%
if %ss% lss 10 set ss=0%ss%
if %cc% lss 10 set cc=0%cc%
set DURATION=%hh%:%mm%:%ss%.%cc%
echo Start : %function_starttime%
echo Finish : %function_endtime%
echo ---------------
echo Duration : %DURATION%
echo.
sqlcmd -S server -i "\\path2\query.sql"
pause
【问题讨论】:
标签: windows batch-file for-loop time batch-processing