How can i use a script like this to record the time it takes for each iteration of a for loop inside a batch script? I've implemented this to record an entire script's execution time but I'd like to get durations for each iteration of a for loop.
@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%
Output:
Start : 11:02:45.92
Finish : 11:02:48.98
---------------
Duration : 00:00:03,06
EDIT: This is how my implementation looks
@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
Set the starttime and endtime variables at either end of the inside of your for loop code block, and put all of the conversion and output code into a function. You'll also need to enable delayed expansion.
@echo off
setlocal enabledelayedexpansion
cls
rem ****************** MAIN CODE SECTION
for /l %%A in (1,1,5) do (
set STARTTIME=!TIME!
rem Your code goes here (remove the ping line)
ping -n 3 -w 1 127.0.0.1 >NUL
set ENDTIME=!TIME!
call :GetDuration !STARTTIME! !ENDTIME!
)
rem ****************** END MAIN CODE SECTION
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.